黑马程序员技术交流社区
标题:
结构体部分的 谁给讲一下 最好逐语句
[打印本页]
作者:
aSmallStone
时间:
2015-5-24 21:58
标题:
结构体部分的 谁给讲一下 最好逐语句
#include #include #include struct student{ char *name; int score; struct student* next;}stu,*stu1; int main(){ stu.name = (char*)malloc(sizeof(char)); strcpy(stu.name,"Jimy"); stu.score = 99; stu1 = (struct student*)malloc(sizeof(structstudent)) stu1->name =(char*)malloc(sizeof(char)) stu.next = stu1; strcpy(stu1->name,"Lucy"); stu1->score = 98; stu1->next = NULL; printf("name %s, score %d \n ",stu.name, stu.score); printf("name %s, score %d \n ",stu1->name, stu1->score); free(stu1); return 0;}
作者:
zhangshuai
时间:
2015-5-25 15:15
这么乱 让人咋看?
作者:
jingkai
时间:
2015-5-25 17:17
这么乱??自己先整理好代码重发下吧亲
作者:
yuang4074
时间:
2015-5-25 17:29
#include <stdio.h> //导入头文件
struct student{ //定义结构体
char *name; //定义一个指向字符的指针变量
int score; //定义一个分数变量
struct student* next;//定义指向结构体类型 student的指针变量
}stu,*stu1; //定义了一个 student结构体变量和一个指向结构体类型 student的指针变量
int main(){ //main函数
stu.name = (char*)malloc(sizeof(char)); //动态给结构体stu成员变量name分配一个字符的空间
strcpy(stu.name,"Jimy"); //将Jimy复制到name指向的空间 ,相当于赋值
stu.score = 99; //给学生结构体stu分数成员变量赋值99
stu1 = (struct student*)malloc(sizeof(struct student)) //动态给结构体stu1分配一个结构体student的空间
stu1->name =(char*)malloc(sizeof(char)) //动态给结构体stu1成员变量name分配一个字符的空间
stu.next = stu1; //结构体stu的下一个指针指向结构体stu1
strcpy(stu1->name,"Lucy"); //将Lucy复制到name指向的空间 ,相当于赋值
stu1->score = 98; //给学生结构体stu1分数成员变量赋值99
stu1->next = NULL; //结构体stu的下一个指针指向null,相当于不指向任何变量
printf("name %s, score %d \n ",stu.name, stu.score); //打印结构体stu的成员变量name和score
printf("name %s, score %d \n ",stu1->name, stu1->score); //打印结构体stu1的成员变量name和score
free(stu1); //释放结构体stu1的占有的空间
return 0;
}
复制代码
作者:
Residual_mem
时间:
2015-5-25 18:08
C还是OC的?
作者:
aSmallStone
时间:
2015-5-26 18:38
你看呢。。。:L
作者:
yuang4074
时间:
2015-5-26 18:41
aSmallStone 发表于 2015-5-26 18:38
你看呢。。。
自己学会总结
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2