Passing an array of structures in C

I am unable to pass an array of structures to a function in C.

I created the structure as follows:

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

Then I refer to it like this: MyItems[0].stock = 10; etc.

I want to pass it to such a function:

  ReadFile(MyItems); 

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

I tried a bunch of declarations, but none of them work. eg

 void ReadFile(struct Items[10]) 

I was looking for other questions, but the fact is that they are all made different, with typedefs and asterisks. My teacher has not taught us signs yet, so I would like to do this with what I know.

Any ideas ?: S

EDIT: Salvatore's answer works after I corrected my prototype:

 void ReadFile(struct Items[10]); 
+7
source share
9 answers
 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 works fine in my compiler. Which compiler are you using? What is your mistake?

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

+7
source

Define struct Items outside the main. When passing an array to a function in C, you must also pass the length of the array, since there is no way to find out how many elements are in this array (unless it is guaranteed to be a fixed value).

As Salvatore said, you also need to declare (not necessarily define) any structures, functions, etc., before you can use them. Usually you have your structures and function prototypes in the header file in a larger project.

The following 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; } 
+3
source

The function will not know that the struct Items type exists if you declare it only locally inside the body area of ​​the main function. Therefore, you must define the structure from the 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 because ReadFile has no idea how large the array is (arrays are always passed by decay-to-pointer). Therefore, you usually add this information:

 void ReadFile(struct Items * arr, size_t len); ReadFile(my_items, 10); 
+1
source

Why don't you use a passing pointer to an array to the methods that need it?

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

void ReadFile (struct Items * items);

and where do you call him

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

You need to be careful with pointers ...

0
source

You should pretty much use pointers for this. You will look like this:

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

You need to use an array pointer, after which it's easy to access its members

void ReadFile (Items * items);

must work.

0
source

Well, when you pass the structure as you did, it actually creates a local copy of this function. Thus, this will not affect your original structure, no matter how you modify it in the ReadFile .

I am not sure of a different approach, and this may not answer your question, but I recommend that you try the pointers. You will definitely use them quite a lot in C / C ++. And they can be really powerful once you master them.

0
source

You tried to declare that you are functioning as follows:

 void ReadFile(struct Items[]) 

May be useful: http://www.daniweb.com/software-development/cpp/threads/105699

0
source

Instead of declaring, declare this way:

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

and such a function:

 void ReadFile(Items *items); 

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

0
source

All Articles