How to pass an array of struct using pointer in c/c++?

无人久伴 提交于 2019-12-18 02:48:15

问题


in C code I'm stuck to pass an array of struct to a function, here's the code that resembles my problem:

typedef struct
{
   int x;
   int y;
   char *str1;
   char *str2;
}Struct1;

void processFromStruct1(Struct1 *content[]);
int main()
{
    Struct1 mydata[]=
    { {1,1,"black","cat"},
      {4,5,"red","bird"},
      {6,7,"brown","fox"},
    };

    processFromStruct1(mydata);//how?!?? can't find correct syntax

    return 0;
}

void processFromStruct1(Struct1 *content[])
{
    printf("%s", content[1]->str1);// if I want to print 'red', is this right?
        ...
}

Compile error in msvc is something like this:

error C2664: 'processFromStruct1' : cannot convert parameter 1 from 'Struct1 [3]' to 'Struct1 *[]'
1>       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

How to solve this? tnx.


回答1:


You almost had it, either this

void processFromStruct1(Struct1 *content);

or this

void processFromStruct1(Struct1 content[]);

and, as Alok points out in comments, change this

content[1]->str1

to this

content[1].str1

Your array is an array of structures, not an array of pointers, so once you select a particular structure with [1] there is no need to further dereference it.




回答2:


Try

processFromStruct1( & mydata[ i ] ); // pass the address of i-th element of mydata array

and the method to

void processFromStruct1(Struct1 *content )
{
    printf("%s", content->str1);
        ...
}

(2nd part already noted by John Knoeller and Alok).




回答3:


John Knoeller gave the perfect syntax , I am trying to explain some basic things, I hope that it willsolve your confusions in future. This is very similar to passing pointer to a function in C. Of course struct is also a pointer,

so we can pass the value in 2 ways 0. Via pointer 0. Via array ( since we are using array of struct )

so the problem is simple now , You have to give the data type of a variable as we do in normal pointers , here the data type is user-defined ( that means struct ) Struct1 then variable name, that variable name can be pointer or array name ( choose a compatible way ).




回答4:


This works for me. Changed structs to C++ style.

struct Struct1
{
    int x;
    int y;
    char *str1;
    char *str2;
};

Struct1 mydata[]=
{   {1,1,"black","cat"},
    {4,5,"red","bird"},
    {6,7,"brown","fox"},
};

void processFromStruct1(Struct1 content[]);

int main()
{
    processFromStruct1(&mydata[1]);    
    return 0;
}

void processFromStruct1(Struct1 content[])
{
    printf("%s",content->str1);
}

output: red




回答5:


You can try the prototype as void processFromStruct1(Struct1 content[]); and then the declaration should be like void processFromStruct1(Struct1 content[]).



来源:https://stackoverflow.com/questions/2360794/how-to-pass-an-array-of-struct-using-pointer-in-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!