开发者

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

in C code I'm stuck to pass an array of struct to a function, he开发者_StackOverflow社区re'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.


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.


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


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


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


Perhaps a proper re-factoring from the future:

#include <stdio.h>

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

static void proc_the_struct_1_arr (
    const int count_ ,
    // array arg declared with min number of arguments
    // also can not be null
    struct_1 content[ static count_ ]
    )
{
    for (unsigned j = 0; j < count_; ++j)
    printf("x:%-4dy:%-4d%-12s%-12s\n", content[j].x,content[j].y,content[j].str1,content[j].str2);
}
int main( void )
{
    struct_1 mydata[3]= { 
      {.str1 = "black", .str2 = "cat" },
      {.str1 = "red",   .str2 = "bird"},
      {.str1 = "brown", .str2 = "fox" },
    };

    proc_the_struct_1_arr (3,mydata);

    return 0;
}

Godbolt

Obviously proc_the_struct_1_arr declaration is interesting. That actually uses Variably Modified Types (VMT). That is a legal syntax only for array arguments.

That is not slower vs the pointer to array solution:

static void proc_the_struct_1_arr_pointer (
    const int count_ ,
    // array arg not declared with min 
    // required number of arguments
    struct_1 (*arr_ptr)[ /* static no allowed here */ count_ ]
    );

I assume the compiler "rewrites" the above to the array pointer, anyway. On the second option arr_ptr can be a null argument.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜