单向链表
书上的单向动态链表程序 p1 p2定义成指向结构体struct student的指针 为什么scanf 里还需要加&(注释?的那行) p1 p2不是已经是地址了么#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num; //学号
float score; //成绩
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
]struct student *p1,*p2; //指向struct student 的指针p1 p2
p2=p1=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num, &p2->score); //???????????、
head=NULL;
n=0;
while(p1->num!=0)
{
n=n+1;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
p2=p1;
}
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return (head);
}
void print(struct student *head) //输出函数
{
struct student *p;
printf("you %d ge shu jv:\n",n);
p=head;
if(head!=NULL)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
}
void main() //主
{
struct student *head;
head=creat();
print(head);
} p 是指针,不是地址。也就是说 p 所存储的资料是 student 结构的地址,但它本身有自己的地址。 scanf 是把输入资料写入相对应的地址。基本上 scanf 要配合 &,就跟 int main() 配合 return 0 一样 claws0n 发表于 2018-8-17 16:23
p 是指针,不是地址。也就是说 p 所存储的资料是 student 结构的地址,但它本身有自己的地址。 scanf 是把 ...
是不是应该从 运算符优先级 来看 & 取的是 p1->num 这个整体而不是单取p1 本帖最后由 claws0n 于 2018-8-18 10:53 编辑
HISIOISIH 发表于 2018-8-17 19:31
是不是应该从 运算符优先级 来看 & 取的是 p1->num 这个整体而不是单取p1
更正:是的 claws0n 发表于 2018-8-17 23:08
更正:是的
谢谢
页:
[1]