结构体成员和结构体指针初始化
2015-10-20 16:48
337 查看
#include
#include
#include
struct student{
char *name;
int score;
struct student* next;
}stu,*stu1;
int main(){
stu.name = (char*)malloc(sizeof(char)); /*1.结构体成员指针需要初始化*/
strcpy(stu.name,"Jimy");
stu.score = 99;
stu1 = (struct student*)malloc(sizeof(struct student));/*2.结构体指针需要初始化*/
stu1->name = (char*)malloc(sizeof(char));/*3.结构体指针的成员指针同样需要初始化*/
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;
}
#include
#include
struct student{
char *name;
int score;
struct student* next;
}stu,*stu1;
int main(){
stu.name = (char*)malloc(sizeof(char)); /*1.结构体成员指针需要初始化*/
strcpy(stu.name,"Jimy");
stu.score = 99;
stu1 = (struct student*)malloc(sizeof(struct student));/*2.结构体指针需要初始化*/
stu1->name = (char*)malloc(sizeof(char));/*3.结构体指针的成员指针同样需要初始化*/
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;
}
相关文章推荐
- Swift 中什么时候用? 什么时候用!
- oracle 死锁
- cocoaPods安装以及使用过程
- iOS gif图显示问题
- Tomcat性能优化调整
- C++ 11 auto & decltype
- 分布式发布订阅消息系统 Kafka 架构设计 - 目前见到的最好的Kafka中文文章
- iOS设计模式——Category
- iOS 打开网页URL
- libevent linux安装
- 启动多线程以及延迟输出内容代码
- 高级软件工程课程第一次作业的小结
- lombok造成的StackOverflowError
- TatukGIS Editor使用教程:位置显示和三角测量
- jQuery 遍历- 关于closest() 的方法介绍以及与parents()的方法区别分析
- java反射及动态代理
- iOS设计模式——单例模式
- android 通知的使用(Notification)
- 【转】Oracle存储过程学习
- 3D数学--矩阵知识