typedef struct data
{char num[5];
char name[20];
char phonenum[14];
}*DataType;
typedef struct node
{
DataType data;
struct node *next;
}*ListNode;
ListNode newnode = (ListNode)malloc(sizeof(struct node));
if (newnode == NULL)
{
printf("内存错误!\n");
return FALSE;
}
printf("编号: ");
scanf("%s", newnode->data->num);
这段代码在运行过程中输入编号时会报“段错误”。
因为在ListNode newnode = (ListNode)malloc(sizeof(struct node));时只是为newnode这个struct node型的指针分配了内存空间。
代表其中struct data型的指针有了内存空间,但是date所指向的这段内容并没有分配内存空间。所以如果要以这种方式输入,必须先为newnode->data分配内存空间再输入。
或者可以把DateType前的*去掉,输入时scanf("%s", newnode->data.num);来进行输入更为方便。