C语言结构体必知必会
来源:互联网 发布:mac os 最新版本号 编辑:程序博客网 时间:2024/06/08 13:28
结构体
结构体是一种特殊类型,可以打包其它类型为一种复合类型。在面向对象的概念中,就是一种特殊类。
使用结构体几种形式:
第一种
先定义结构体,然后定义结构体变量。
定义结构体:
struct point1{ int x; int y;};
定义结构体变量
struct point1 point;
第二种
定义匿名结构体,然后定义结构体变量
struct{ int x; int y;}point2;
第三种
定义结构体的同时定义结构体变量
struct point3{ int x; int y;}point;
第四种
用typedef定义结构体
typedef struct point4{ int x; int y;}t_point;
然后用t_point定义结构体变量
t_point point;
结构体数组
struct student{ int age; char *name; }; struct student ss[10];
结构体指针
struct student *pst; pst = &foo;
结构体初始化
有结构体定义
struct student{ int age; char *name; };
第一种
struct student foo1 = {11, "xiaoming"}; struct student foo2 = {11}
第二种
struct student foo3 = {.age = 11};
第三种
struct student foo4 = (struct student){11, "xiaoming"}; struct student foo5 = (struct student){.age = 11};
结构体数组初始化
struct student ss1[10] = {0}; struct student ss2[10] = {{}, {}, {}}; struct student ss3[10] = {[2] = {}, [3] = {}}; struct student ss4[10] = {[2].age = 10, [3].name = "xiaoming"};
访问结构体成员
使用“.”返回结构体成员
struct student foo = {11, "xiaoming"}; int age = foo.age; char *name = foo.name; printf("age is %d, name is %s\n", age, name); foo.age = 20; foo.name = "liyong"; printf("age is %d, name is %s\n", foo.age, foo.name);
当使用结构体指针的时候可以用箭头操作符”->”
struct student *pst; pst = &foo; printf("pst age is: %d and name is %s\n", (*pst).age, (*pst).name); printf("pst age is: %d and name is %s\n", pst->age, pst->name);
0 0
- C语言结构体必知必会
- C语言中的结构
- C语言结构体
- C语言:结构体
- C语言字段结构
- C语言:结构体
- C语言语句结构
- C语言进程结构
- C语言中的-------结构
- C语言程序控制结构
- C语言--结构体
- c语言结构体
- c语言 结构体
- C语言结构体
- C语言结构体
- C语言文件结构
- C语言--结构体
- c语言 结构体
- 解决不能远程连接Linux服务器上MySQL的问题
- Memcache常用方法
- 【Android应用开发技术:用户界面】ActionBar
- javascript事件绑定
- C语言typedef 关键字
- C语言结构体必知必会
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- 【Android应用开发技术:用户界面】其他杂项组件
- UVA 1462 or UVALive 4769 Fuzzy Google Suggest Trie树上的dfs 2009年哈尔滨G题
- Java学习笔记之 File构造方法
- 黑马程序员——Java基础——对象序列化、管道流、RandomAccessFile类、操作基本数据类型的流对象、字符编码
- 新版JQuery Mobile返回按钮
- P122.17
- input IDC