【发布时间】:2017-10-14 05:07:04
【问题描述】:
我编写了以下代码,但是当我尝试编译代码时,编译器显示以下错误。我的错在哪里?
编译器错误:
main.c:32:39:错误:取消引用指向不完整类型“结构信息”的指针 printf("信息:%d\n", ptr->_number);
#include <stdio.h>
#include <stdlib.h>
typedef struct Informaion{
int _number;
struct Informaion *next;
} Information;
int main(int argc, char const *argv[]){
Information *temp;
Information *head;
temp = malloc(sizeof(Information));
temp->_number = 20;
head = temp;
temp = malloc(sizeof(Information));
temp->_number = 21;
head->next = temp;
temp = malloc(sizeof(Information));
temp->_number = 22;
head->next->next = temp;
temp = malloc(sizeof(Information));
temp->_number = 23;
head->next->next->next = NULL;
struct Information *ptr = head;
while(ptr != NULL) {
printf("Information : %d\n", ptr->_number);
ptr = ptr->next;
}
free(head);
free(temp);
return 0;
}
【问题讨论】:
-
你的结构中有很多印刷错误。我不知道您要做什么,因为您没有说出来,但是如果您想打印地址,请使用
%p而不是%d -
我想显示我为结构设置的数据。我修好了。
-
@MikhailRazborov 如果您的问题得到解决,您可以接受一个答案。
-
在调用任何堆分配函数时:(malloc, calloc, realloc),始终检查 (!=NULL) 返回值以确保操作成功
-
不使用
main()的参数时,使用备用签名:int main( void )
标签: c compiler-errors singly-linked-list dereference