C取消引用指向不完整类型的指针时出错
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上,在指向struct的指针上使用->,因此请改用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));
}
相关文章:
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- (C )找到基本类型指针的儿童类型
- 表达式必须具有指向对象的指针类型(指针向量)
- 将子项复制构造到父类型指针中
- C 返回类型指针声明
- 调用虚拟函数而不通过类类型指针创建任何对象
- C++ 如何使用类类型指针制作向量
- 为什么基类型指针不能获取派生类对象的地址值?
- 如何在 C++ 中获取映射类型指针,映射
- 如何检查该类型的类型指针是正确对齐的
- 正在转换为短类型指针
- 指向任意类方法的模板非类型指针
- 如何通过强制转换类型指针将字符数组转换为uint16_t
- 指向shared_ptr的不透明类型 C 指针
- C++:从值类型指针强制转换为包含迭代器
- C/C++:访问给定类型指针的位置与访问另一类型指针的相同位置不同
- 比较类型指针
- 数据类型指针使用*(Datatype *)
- 如何打印c++中char类型指针的所有值