November 22nd, 2013, 09:38 AM
Array of struct
I'm getting the error:
void processFromStruct1(Struct1 content)
What is wrong with my code?
test.c: In function ‘main’:
test.c:26:5: aviso: passing argument 1 of ‘processFromStruct1’ from incompatible pointer type
test.c:13:6: nota: expected ‘struct Struct1 *’ but argument is of type ‘struct Struct1 (*)’
November 22nd, 2013, 09:49 AM
remove the "&" (ampersand) before "mydata". Array is the pointer itself, you need not take its address once more.
- programming problems for novice coders
November 22nd, 2013, 11:35 AM
To offer a broader explanation, an array name is generally the same thing as a pointer to the first element of the array; ie:
mydata == &mydata
Therefore, your function call should have been either
with the latter being preferred. As you can see, writing &mydata (as opposed to &mydata) added an extra level of indirection which doesn't even make any sense.
The equivalence of array names and pointers can only be taken so far. The main difference is that you cannot change where an array name points to. So while you can assign a different address to a pointer, you cannot do the same to an array name. And while you can increment or decrement a pointer, you cannot do the same to an array name. Other than that (unless I forgot one), you can safely treat an array name as a pointer and vice versa (eg, use array indexing notation on a pointer).