2013-10-05 146 views
1

我有一個名爲S的結構體和一個指向結構體S的名爲A的指針數組。我的函數T將一個指向結構體S的指針作爲參數。向前聲明一個指向結構的指針數組?

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(); 
} 

隨着打印語句,我能看到A [I-1]是一個類型的struct的,但是沒有指針至S這正是我想要的。這可能是因爲我轉發宣告A嗎?

回答

1
struct S *A; // this is a pointer of struct S type. 

需要聲明

struct S **A; // pointer to pointer 

struct S *A[MAX_SIZE]; //array of pointers 
0
struct S *A; 

申報需要

struct S *A[10]; 


A[i] = malloc(sizeof(S)); 
指針數組
相關問題