#### 結構體
```c
struct student
{
const char *name;
const char *address;
int age;
};
```
struct student表示一個類型,類似于int,聲明和初始化,以及使用如下:
```c
#include <stdio.h>
#include <string.h>
struct student
{
const char *name;
const char *address;
int age;
};
int main(int argc, char const *argv[])
{
struct student person = {"wuzhc", "Guangzhou", 18};
printf("%s\n", person.name);
return 0;
}
```
#### typeof別名
可以為結構體設置別名,如下
```c
typedef struct student
{
const char *name;
const char *address;
int age;
} stu;
```
其中結構體名可以省略,如下:
```c
typedef struct
{
const char *name;
const char *address;
int age;
} stu;
```
#### 結構體復制
當一個結構體變量賦值給另一個結構體變量時,計算機會創建一個全新的副本,如果結構體含有指針,那么復制的僅僅是指針的值,如下:
#### 嵌套結構
```c
#include <stdio.h>
#include <string.h>
typedef struct
{
const char *subject;
int score;
} report;
typedef struct
{
const char *name;
const char *address;
int age;
report card; // 這是一個嵌套結構體
} stu;
int main(int argc, char const *argv[])
{
stu person = {"wuzhc", "Guangzhou", 18, {"english", 59}};
printf("%s\n", person.card.subject);
return 0;
}
```
#### 結構體指針
類似于其他類型指針,代碼如下:
```c
void modifyAge(stu *s)
{
s->age = 23;
// 等價于(*s).age = 23
}
```
#### 聯合union
當定義聯合時,計算機會為最大的字段分配空間,如php的zend_value如下:
```c
typedef union _zend_value {
zend_long lval; //int整形
double dval; //浮點型
zend_refcounted *counted;
zend_string *str; //string字符串
zend_array *arr; //array數組
zend_object *obj; //object對象
zend_resource *res; //resource資源類型
zend_reference *ref; //引用類型,通過&$var_name定義的
zend_ast_ref *ast; //下面幾個都是內核使用的value
zval *zv;
void *ptr;
zend_class_entry *ce;
zend_function *func;
struct {
uint32_t w1;
uint32_t w2;
} ww;
} zend_value;
```
初始化:
```c
zend_value zv = {.dval=1.5}
```
#### 枚舉
關鍵字enum,定義如下:
```c
typeof enum {
COUNT, POUNDS, PINTS
} unit;
```
#### 位字段
用位字段主要用于減少占用空間,如定義一個結構如下:
```c
typedef struct
{
unsigned int fist_visit:1;
unsigned int come_age:1;
unsigned int fingers_lost:4;
unsigned int days_a_week:3;
} survey;
```
位字段應當聲明為unsigned int;只有出現在同一個結構中,位字段才能節省空間
#### 位字段和數值計算
最大值 = 2^位數 - 1,即4位只能保存0到15的值
#### demo
```c
#include <stdio.h>
#include <string.h>
// 枚舉
typedef enum
{
PREPATE_TYPE, HOMEWORK_TYPE, SCANEXAM_TYPE
} sourceType;
// 聯合
typedef union
{
short count;
short weight;
} quantity;
// 結構體
typedef struct
{
const char *subject;
int score;
} report;
// 結構體
typedef struct
{
const char *name;
const char *address;
int age;
report card; // 這是一個嵌套結構體
sourceType type;
quantity q;
} stu;
// 位字段
typedef struct
{
unsigned int fist_visit:1;
unsigned int come_age:1;
unsigned int fingers_lost:4;
unsigned int days_a_week:3;
} survey;
// 結構體指針
void modifyAge(stu *s)
{
s->age = 23;
// 等價于(*s).age = 23
}
int main(int argc, char const *argv[])
{
stu person = {"wuzhc", "Guangzhou", 18, {"english", 59}, HOMEWORK_TYPE, {.weight=118}};
if (person.type == HOMEWORK_TYPE) {
printf("%s\n", "homework");
} else {
printf("%s\n", "other");
}
modifyAge(&person);
printf("%i\n", person.age);
printf("%i\n", person.q);
return 0;
}
```
- php
- 編譯安裝
- 基本概念
- 垃圾回收機制
- 生命周期
- zval底層實現
- c擴展開發
- gdb調試工具
- 自定義擴展簡單demo
- 鉤子函數
- 讀取php.ini配置
- 數組
- 函數
- 類
- yaf擴展底層源碼
- swoole擴展底層源碼
- memoryGlobal內存池
- swoole協程使用記錄
- 單點登錄sso原理
- compser使用
- session實現機制
- c & linux
- gcc
- 指針
- 結構體,聯合和位字段
- 宏定義井號說明
- printf家族函數和可變參數
- 共享函數
- 靜態庫和動態庫
- makefile自動化構建
- 信號一
- 信號二
- inotify監控文件事件
- socket編程
- 簡介
- UNIX DOMAIN
- Internet DOMAIN
- TCP/IP
- 文件IO多路復用
- 內存管理
- 進程組,會話和控制終端
- daemon守護進程
- 多進程
- 多線程
- 常用進制轉換
- go
- 入門知識
- 字節和整數裝換
- python
- redis
- 應用場景
- 消息隊列
- 熱點數據
- 掃碼登錄
- 訂閱發布
- 次數限制
- 搶購超賣
- 持久化機制
- mysql
- 工作流程
- MyISAM和InnoDB區別
- 用戶和權限管理
- 執行計劃
- sql優化
- 事務和鎖
- 慢查詢日志
- case...when...then...end用法
- sql
- 參考
- linux
- 內核參數優化
- 防火墻設置
- docker
- docker入門知識
- 算法
- 多維數組合
- DFA算法
- 紅包金額分配