搜尋此網誌

2015年7月20日 星期一

C typedef struct基本使用方式

struct的使用

1. 基本用法,最後加上;
struct test {
    bool test_1;
    bool test_2;
};

宣告:
struct test a;

2.基本用法_2, 宣告時順便定義變數
struct test {
    bool test_1;
    bool test_2;
}; a

typedef + struct的用法

3.基本用法
typedef struct {
    bool test_1;
    bool test_2;
} test;

宣告:
test a;

4.宣告時候順便宣告變數
typedef struct {
    bool test_1;
    bool test_2;
} test, a; //等同於 test a;

5.宣告pointer struct
typedef struct {
    bool test_1;
    bool test_2;
} test, *p_test;

宣告變數 //此宣告等同於 test* a;
p_test a;

存取成員變數
a->test_1 = true;