Passing an array of structs in C

后端 未结 9 841
傲寒
傲寒 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:27

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

    void ReadFile(Items * items);

    should work.

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

    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

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

    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

    0 讨论(0)
提交回复
热议问题