Passing an array of structs in C
Posted
by
lelouch
on Stack Overflow
See other posts from Stack Overflow
or by lelouch
Published on 2011-11-21T00:27:58Z
Indexed on
2011/11/21
1:51 UTC
Read the original article
Hit count: 233
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[9]);
© Stack Overflow or respective owner