9

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
  • 2
    @mhd: though your question has been asnwered, i suggest you to read some book before jumping to code. reading a book will greatly supplement your understanding. Commented Mar 2, 2010 at 4:02

6 Answers 6

16

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.

Sign up to request clarification or add additional context in comments.

4 Comments

and content[1]->str1 becomes content[1].str1.
He may also want to pass a length.
...which can be obtained by passing sizeof(mydata)/sizeof(mydata[0]), but only before the function call
@Adam: ...or after the call, but not in the call :-).
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).

2 Comments

Then one can't index that pointer with 1, one can only use 0. Not sure if this is what the OP wants. Also the type is wrong.
OK, now I am happy with the answer. +1.
1

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

Comments

0

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

1 Comment

That should not compile and is not "C++ style". Above the keyword, struct must precede the Struct1 name. Example: void processFromStruct1(struct Struct1 content[]);
0

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.

Comments

-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.