Reputation: 219
Basically I have something like this in my code
struct connComp {
struct connComp *parent;
struct connComp *neigh[noNN];
int *pSpinValue, SpinValue, Flag;
unsigned int size;
} comp[N];
and when I try to use the array of structures comp[N]
as input to a function of the type void function(struct connComp)
by writing function(comp)
in my code, I receive the following error from gcc compiler:
incompatible type for argument 1 of ‘function’ function(comp)
expected ‘struct connComp’ but argument is of type ‘struct connComp *’
So it looks like comp[N]
was declared as a pointer and I really cannot figure out why. Thanks very much in advance for any help!
Upvotes: 2
Views: 59
Reputation: 1916
Passing pointers and arrays to a function and C is equivalent. comp
is an array of N connComp
structs. When you pass comp
to the function you have the address of the start of the array, which behaves in the same way a pointer does. To use comp
in a function that expects the struct, you have to dereference the pointer - try passing in comp[0]
.
Upvotes: 2
Reputation: 1024
An array in C is a pointer. Specifically, to the first element of the array.
To pass a single element to your function, you need to specify which element that is. e.g.
function(comp[2]);
If you want to work on the entire array in your function, you need to change the function to accept an array of your struct, or a pointer to your struct. e.g.
void function(struct connComp[N]); /* to receive an array of a static size */
void function(struct connComp*); /* to receive an array of a variable size */
Upvotes: 1