简体   繁体   中英

Passing an array of structs in C

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. eg

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]);
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.

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;
}

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);

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...

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

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

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

void ReadFile(Items * items);

should work.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM