what is diffences betweent link1.cpp and link2.cpp
link1.cpp
Code:
#include <stdio.h>
//#define NULL 0
struct student
{
long num;
float score;
struct student *next;
};
int main()
{
struct student a,b,c,*head,*p;
a.num = 99101;
a.score = 89.5;
b.num = 99103;
b.score = 85.0;
c.num = 99105;
c.score = 90.0;
head = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
p = head;
do
{
printf("%ld %5.1f\n",p->num,p->score);
p = p->next;
}while(p != NULL);
}
link2.cpp
Code:
#include <stdio.h>
#include <stdlib.h>
//#define NULL 0
struct student
{
long num;
float score;
//struct student *next;
};
int main()
{
struct student a,b,c;//*head,*p;
struct student * p = (struct student *) malloc(3*sizeof(struct student));
struct student * q = p;
p->num = 99101;
p->score = 89.5;
p++;
p->num = 99103;
p->score = 85.0;
p++;
p->num = 99105;
p->score = 90.0;
int i = 0;
do
{
printf("%ld %5.1f\n",q->num,q->score);
//p = p->next;
q++;
i++;
}while(q->num != 0 && i < 3);
}