Passing an array of structs in C

后端 未结 9 840
傲寒
傲寒 2020-12-01 16:33

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
    {
          


        
相关标签:
9条回答
  • 2020-12-01 17:11

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

    0 讨论(0)
  • 2020-12-01 17:13
    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.

    0 讨论(0)
  • 2020-12-01 17:13

    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;
    }
    
    0 讨论(0)
  • 2020-12-01 17:15

    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.

    0 讨论(0)
  • 2020-12-01 17:23

    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);
    
    0 讨论(0)
  • 2020-12-01 17:24

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

    void ReadFile(Items * myItems, int numberOfItems) {
    }
    
    0 讨论(0)
提交回复
热议问题