[英]C Error for dereferencing pointer to incomplete type
我的主文件有一些問題。我有一個結構矩陣(理論上),我想修改所有結構中的所有“ p”參數。 這是主文件:
int main(int argc, char** argv) {
int i, j;
struct PQ *queue;
queue = createQ(5);
for (i = 0; i <= 5; i++) {
for (j = 0; j = 20; j++);
queue->mem[i][j].p = 1;
}
for (i = 0; i <= 5; i++) {
puts("\n");
for (j = 0; j <= 20; j++);
printf("%d ",queue ->mem[i][j].p);
}
return (EXIT_SUCCESS);
}
這是另一個包含結構定義和生成函數的文件:
typedef struct newLine{
unsigned p;
} newLine;
struct PQ{
struct newLine ** mem;
};
struct PQ *createQ(unsigned min){
int i=0;
struct PQ *newQ = malloc(sizeof(PQ));
newQ->mem = malloc(min*sizeof(newLine *));
for(i=0;i<=min;i++){
newQ->mem[i]=calloc(20,sizeof(newLine));
}
return newQ;
}
有任何想法嗎?
您會看到此錯誤,因為struct是C中類型的一部分,因此您必須在struct PQ中使用struct newline
。 另一種方法是使用typedef創建類型別名:
typedef struct newline {
int p;
} newline;
訪問struct成員的方法是use。 在結構上,使用->指向結構的指針,因此請使用queue->mem[i][j].p
。
還有其他問題。
您不能取消引用未初始化的指針,它會產生未定義的行為:
PQ *newQ;
應該:
struct PQ *newQ = malloc(sizeof(struct PQ));
您應該使用正確的間接分配:
newQ->mem = malloc(min * sizeof(newline*));
for(int I = 0;i < min; i++){
newQ->mem[i] = calloc(20, sizeof(newLine));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.