17

I'm having trouble passing an array of structs to a function in C.

I've created the struct like this in main:

int main()
{
    struct Items
    {
        char code[10];
        char description[30];
        int stock;
    };

    struct Items MyItems[10];
}

I then access it like: MyItems[0].stock = 10; etc.

I want to pass it to a function like so:

 ReadFile(MyItems);

The function should read the array, and be able to edit it. Then I should be able to access the same array from other functions.

I've tried heaps of declarations but none of them work. e.g.

void ReadFile(struct Items[10])

I've had a look around for other questions, but the thing is they're all done different, with typedefs and asterisks. My teacher hasn't taught us pointers yet, so I'd like to do it with what I know.

Any ideas? :S

EDIT: Salvatore's answer is working after I fixed my prototype to:

void ReadFile(struct Items[10]);
3
  • It is "real code". I'm using it with Visual studio 2010 and it works. :S Commented Nov 21, 2011 at 0:30
  • 2
    "Visual Studio" and "it works" shall not be used in one sentence in polite conversation. You must say explicitly if you are talking about specific non-standard platforms; otherwise people will assume you are talking about the standard language. Commented Nov 21, 2011 at 0:32
  • 2
    Ok I fixed it. Hopefully that's real enough >.< Commented Nov 21, 2011 at 0:33

9 Answers 9

19
struct Items
{
    char code[10];
    char description[30];
    int stock;
};

void ReadFile(struct Items items[10])
{
    ...
}

void xxx()
{
    struct Items MyItems[10];
    ReadFile(MyItems);
}

This in my compiler works well. What compiler are you using? What error you got?

Remember to declare your struct before your functions or it will never work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked, after I fixed my prototype to void ReadFile(struct Items[10]);
8

Define struct Items outside of main. When passing an array to a function in C, you should also pass in the length of the array, since there's no way of the function knowing how many elements are in that array (unless it's guaranteed to be a fixed value).

As Salvatore mentioned, you also have to declare (not necessarily define) any structs, functions, etc. before you can use them. You'd usually have your structs and function prototypes in a header file in a larger project.

The below is a working modification of your example:

#include <stdio.h>

struct Items
{
    char code[10];
    char description[30];
    int stock;
};

void ReadFile(struct Items items[], size_t len)
{
    /* Do the reading... eg. */
    items[0].stock = 10;
}

int main(void)
{
    struct Items MyItems[10];

    ReadFile(MyItems, sizeof(MyItems) / sizeof(*MyItems));

    return 0;
}

Comments

3

Instead of your declaration, declare in that way:

typedef struct {
        char code[10];
        char description[30];
        int stock;
}Items;

and the function like that:

void ReadFile(Items *items);

With typedef you define a new type, so you don't need to use word "struct" each time.

Comments

3

The function won't know that the type struct Items exists if you declare it only locally inside the main function body scope. So you should define the struct outside:

struct Item { /* ... */ };

void ReadFile(struct Items[]);   /* or "struct Item *", same difference */

int main(void)
{
  struct Item my_items[10];
  ReadFile(my_items);
}

This is dangerous of course since ReadFile has no idea how big the array is (arrays are always passed by decay-to-pointer). So you would typically add this information:

void ReadFile(struct Items * arr, size_t len);

ReadFile(my_items, 10);

8 Comments

Your code has fixed the underlined errors, but it's still not building - I'm getting this: pastebin.com/HxPTcPmL
A) the errors have nothing to do with this part of the code, B) now we're confused about the language again -- suddenly it's C++??
lol, sorry I got the wrong code, try again. That's from trying your code
Try what again? Pastebin doesn't let you change content; you get a new ID instead.
Here's my entire code I'm using in case you're curious: pastebin.com/YAXHdLtM
|
0

You pretty much have to use pointers for this. You function would look like this:

void ReadFile(Items * myItems, int numberOfItems) {
}

Comments

0

You need to use pointer to array, after that its easy to access its members

void ReadFile(Items * items);

should work.

Comments

0

Well, when you pass a structure like you did, it actually creates a local copy of it in the function. So it will have no effect on your original structure, no matter how you modify it in ReadFile.

I am not sure about a different approach and this might not answer your question, but I recommend you try pointers. You'll definitely be using them quite a lot in C/C++. And they can be really powerful once you master them

2 Comments

Not true, you can try that out, C passes array always by pointer, you can modify it in ReadFile, copy is not involved here.
@SalvatorePreviti You are absolutely right! For some odd reason I understood that he wanted to use the structure in his method, not the array. -_-
0

Have you tried to declare you function like this:

void ReadFile(struct Items[])

Might be helpful: http://www.daniweb.com/software-development/cpp/threads/105699

Comments

0

Why dont you use pass the pointer to the array to the methods that need it?

If you want the same struct array then you should use pointer to the array and not pass as array as that will create a copy.

void ReadFile(struct Items * items);

and where you call it

struct Items myItems[10];
ReadFile(myItems);

Need to be careful with pointers...

1 Comment

No copy involved here, arrays are never copied.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.