结构体基本操作
2015-08-05 17:26
399 查看
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <string.h> #include <stdio.h> //定义一个结构体类型 //定义了一个数据类型 。固定大小内存块的别名 还没有分配内存 //类型的重定义 typedef struct Teacher { char name[64]; int age ; int id; }Teacher; //用类型定义变量的方法3种 struct Student { char name[64]; int aga ; }s1, s2 ; //定义类型的同时 定义变量 struct { char name[64]; int aga ; }s3, s4; //匿名类型 定义变量 //初始化变量的三种方法 //定义变量 ,然后初始化 Teacher t3 = {"aaaa", 31, 01}; struct Student2 { char name[64]; int aga ; }s5 = {"names", 21}; struct { char name[64]; int aga ; }s6 = {"names", 21}; void main01() { //struct Teacher t1; //告诉C编译器给我分配内存 Teacher t1; //告诉C编译器给我分配内存//1 Teacher t2 = {"aaaa", 32, 02}; t1.age = 31; //t1. 操作符 是 干什么 有没有操作内存 //是寻址操作 计算age 相对于 t1大变量的 偏移量 ===》计算 cup中进行。。 //没有操作内存 //通过指针的方式 操作 内存空间 { Teacher *p = NULL; p = &t2; printf("p->age:%d \n", p->age); //-> 是寻址操作 算age 相对于 t2大变量的 偏移量 计算 cup中进行。 printf("p->name:%s \n", p->name); } strcpy(t1.name, "t1name"); printf("t1.name:%s \n", t1.name); printf("hello...\n"); system("pause"); return ; } void copyTeacher(Teacher to, Teacher from ) { to = from; } void copyTeacher02(Teacher *to, Teacher *from ) { //(*to) = (*from); *to = *from; } void main111() { Teacher t1 = {"aaaa", 32, 02}; Teacher t2; Teacher t3; memset(&t3, 0, sizeof(t3)); t2 = t1; //=号操作下 编译器的行为 //编译器给我们提供 简单 =号 赋值操作 。。。我们要顺从 printf("t2.name:%s \n", t2.name); printf("t2.age:%d \n", t2.age); copyTeacher(t3, t1); printf("copyTeacher() after \n"); printf("t3.name:%s \n", t3.name); printf("t3.age:%d \n", t3.age); printf("hello...\n"); copyTeacher02(&t3, &t1); printf("copyTeacher02() after \n"); printf("t3.name:%s \n", t3.name); printf("t3.age:%d \n", t3.age); printf("hello...\n"); system("pause"); return ; }
相关文章推荐
- ViewPager 详解(五)-----使用Fragment实现ViewPager滑动
- ViewPager 详解(四)----自主实现滑动指示条
- hdoj.1289 Hat’s IEEE【水题】 2015/08/05
- java静态代理和动态代理(JDK&cglib)
- android studio 下载及配置 Genymotion模拟器
- STL之set集合容器
- Emmet:HTML/CSS代码快速编写神器
- CUDA程序设计(二)
- Android 机顶盒应用开发经验(1)
- 使用Git命令从Github远程仓库更新项目
- Exercise——CopyComplexList
- select、poll、epoll之间的区别总结[整理]
- 编码方式获取Spring中PropertyPlaceholderConfigurer的属性
- 使用UIDataDetectorTypes自动检测电话、网址和邮箱
- 对inverse和cascade的简单理解
- Android中dp,px,sp概念梳理以及如何做到屏幕适配
- WCF 实现播放Flash 详细步骤 -史无前例
- Android中OnkeyDown事件和OnBackPressed方法区别、兼容、使用
- hive优化一
- Java回调机制解析