struct S *A; // this is a pointer of struct S type.
You need to declare
struct S **A; // pointer to pointer
or
struct S *A[MAX_SIZE]; //array of pointers
سؤال
I have a struct named S and an array of pointers to structs S named A. My function T takes a pointer to struct S as an argument.
struct S *A; //forward declare array A of (pointers to) structs
...
void T(struct S *s){//function that accepts pointer to struct S
...
}
void otherFunction(){
...
T(A[i-1]); //Yields error Incompatible type for argument 1
}
int main(){
A = malloc(100 * sizeof(struct S*)); //initialize array
int i;
for(i = 0; i < NumBowls; i++){
A[i] = malloc(100 * sizeof(struct S));//initialize structs in array
}
otherFunction();
}
With a print statement, I was able to see that A[i-1] is of type struct S, but not pointer to S which is what I wanted. Could this because I forward declared A?
المحلول
struct S *A; // this is a pointer of struct S type.
You need to declare
struct S **A; // pointer to pointer
or
struct S *A[MAX_SIZE]; //array of pointers
نصائح أخرى
struct S *A;
to declare array of pointers you need to
struct S *A[10];
A[i] = malloc(sizeof(S));