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));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top