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

cparametersstruct

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.

Best Answer

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.