结构体与结构体指针

下述代码在VS2010上的cpp文件可通过,在杭电OJ上的G++编译项上可通过。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct student{
 4     student *p;
 5     int a;
 6     student()
 7     {
 8         a=100;
 9         p=NULL;
10     }
11 };
12 int main()
13 {
14     student *root=new student();
15     printf("%d\n",root->a);
16     printf("%s\n",root->p);
17     system("Pause");
18     return 0;
19 }

 改变值与改变指向的区别(包括赋值)

 1 #include<stdio.h>
 2 struct node{
 3     node *next;
 4     int num;
 5     node()
 6     {
 7         next=NULL;
 8     }
 9 };
10 void insert(node *a,int x)
11 {
12     a->next=new node();
13     a->next->num=x;
14     return ;
15 }
16 int main()
17 {
18     node *a=new node();
19     a->num=6;
20     insert(a,10);
21     node *p=a;
22     insert(p,8);
23     while(p!=NULL)
24     {
25         printf("%d",p->num);
26         p=p->next;
27         printf("\n");
28     }
29     while(a!=NULL)
30     {
31         printf("%d",a->num);
32         a=a->next;
33                 printf("\n");
34     }
35     return 0;
36 }

 

posted on 2013-08-31 21:21  holyprince  阅读(283)  评论(0编辑  收藏  举报

导航