wei___123
wei___123
2017-10-17 01:02

C语言结构体,我用点不行,必须要用->才编译成功,这是为什么

  • c
  • 链表
  • 结构

#include
#include
#include
#define LEN sizeof(struct student)
struct student
{
int number;
struct student *pnext;
};
int n;
struct student *creat(void)
{
struct student *head,*p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
scanf("%d",&p1.number);
while(p1.number!=0)
{
n++;
if(n==1) head=p1;
else p2->pnext=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d",&p1.number);
}
p2->pnext=NULL;
return(head);
}
main()
{
struct student *p;
p=creat();
while(p->pnext!=NULL)
{
printf("%d\n",p->number);
p=p->pnext;
}
return 0;
}

17 17 E:\学习\C源程序\C红书刷题\第三章_结构数据\链表\创建动态链表\未命名2.cpp [Error] request for member 'number' in 'p1', which is of pointer type 'student*' (maybe you meant to use '->' ?)
这是错误提示,然后我把.改成了->就解决了。为什么要这样, 点和->作用不是一样的么

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答