c語言機構體,枚舉,宏

來源:互聯網
上載者:User

標籤:

結構體:

結構體初始化順序:

先定義結構體,後初始化

定義結構體時同時初始化

匿名自訂初始化結構體

 

計算結構體在記憶體中的記憶體佔用的位元組數:

先找對齊模數,是結構體中的基礎資料型別 (Elementary Data Type)佔用位元組數最大的那個

如果剩下的可以存的下則存,如果存不下則重新分配

如果局部結構體和全域結構體重名則覆蓋全域結構體

 

結構體指標:

struct s{

    int lunxi;

    int speed;

 }car1;

 struct s *p = NULL;

 p = &car1;

 

 

 struct Student stu1 = {18, "張三丰"};

 struct Stucent *p = &stu1;

 兩種方法:

 1) (*p).age   訪問年齡

    (*p).name  訪問姓名

 2) p->age

    p->name

 printf("%s%d", (*p).name, (*p).age);

 printf("%s%d", p->name, p->age);

 

 結構體嵌套:結構體定義的裡面有其他結構體

 結構體不可以嵌套自己變數,可以嵌套指向自己這種類型的指標

 例:

 struct Date{

    int year;

    int month;

    int day;

 }

 

 struct Student{

    char *name;

    int age;

    float score;

    struct Date birthday;

    //struct Student stu; 錯誤的

    struct Student *stu;  正確的

 };

 struct Student stu1 = {"張三丰", 28, 59.99f, {1200, 12, 12}};

 printf("姓名:%s, 年齡:%d(生日:%d-%02d-%02d), 成績:%.2f\n", 

 stu1.name, stu1.age, stu1.birthday.year, stu1.birthday.month, stu1.birthday.day, stu1.score);

 

 struct Person{

    char *naem;

    int age;

    struct Person *child;

 };

 struct Person kim = {"kim", 8, NULL};

 struct Person p1  = {"林志穎", 38, &kim};

 printf("%s的兒子是:%s, 兒子的年齡:%d\n", p1.name, (*p.child).age, p1.childj->name);

 

 用結構體變數做為函數的形參,結構體屬性是改不掉的,實際上還是值傳遞

 結構體指標作為函數形參

 例:

 void xiuche2(struct Car *c1){

    c1->lunzi = 2;

 }

 struct Cat car1 = {4, 200};

 xiuche2(&car1);

 printf("%d\n", car.lunzi);

 

7)枚舉類型:

枚舉類型定義:

 enum iColor {

    white, black, blue

 }

 enum iColor iPhoneColor

 

 enum Sex{Man, Women, Yao} sex; sex取值為 Man, Women, Yao 中的一個

 枚舉變數定義完成後,系統會自動給枚舉的每個元素賦值一個整形的初值,從第一個元素0開始,每個元素加1。//Man = 0; Women = 1; Yao = 2;

 也可以手動賦初值,後面元素自動加一

 

8)typedef:

typedef int ARRAY[5]; //int a[5];數組長度是5

 ARRAY a1, b1;  // int a1[5], b1[5]

 

 int sum(int a, int b){

    return a+b;

 }

 int jian(int a, int b){

    return a-b;

 }

 

 int (*p)(int, int);

 typedef int (*FUN)(int, int); //FUN是一個別名

 FUN f1, f2;

 f1 = sum;

 f2 = jian;

 printf("%d\n", f1(67, 23));

 

9)宏:

宏的分類:

 1)無參宏  #define M 10

 2)有參宏  #define SUM(a) a+a

 int relult = SUM(3); //result = 6;

 #define M1(x, y) x*y+x+y

 result = M1(4, 5); //result = 29;

 

 #define M3(x, y) x = a(a = 2) + 2; y = 4 * 3;

 

 #dfine MAX(a, b) (a > b) ? a : b

 int n = MAX(43, 888);

 

 #define 和 typedef 的區別//宏定義只是簡單的字串代換,是在預先處理完成的,而typedef是在編譯時間處理的,他不是簡單的代換而是對類型說明符重新命名。被命名打標識符具有類型定義說明的功能

 #define INT int*

 typedef int* INT1

 INT a, b; //a是指標變數, b是普通變數

 INT1 a1, b1; //a1是指標變數, b1是指標變數

 

 以#開頭的指令都是預先處理指令

 在編譯器預先處理時,對程式中所有出現的宏名,都用宏定義中的字串去替換,字串中的宏不會替換

 #undef M  //取消宏定義

 

 宏定義可以嵌套

 例:

 #define R 4

 #define PI 3.14

 #define AREA PI*R*R

 

 可以用宏定義表示資料類型,式書寫方便

 例:

 #define P1 printf

 #define P2 "%d\n"

 #define INT int

 #define P struct Person

 P{

 int age;

 char *name;

 }

10)條件編譯:

#if--#elif--#else--#endif

 #ifdef DEBUG1  //如果EDBUG1已定義,則a = 0,否則 a = 10000

    a = 0;      //#ifdef 檢查宏是否定義 DEBUG是系統已定義宏

 #else          //#ifndef 如果沒有定義宏

    a = 10000;

 #endif

 

 if(score < 60) printf("E\n");

 else if(score <= 69) printf("D\n");

 else if(score <= 79) printf("C\n");

 else if(score <= 89) printf("B\n");

 else if(score <= 100) printf("A\n");

 

 #if score <= 60

    printf("E\n");

 #elif score <= 69

    printf("D\n");

 #elif score <= 79

    printf("C\n");

 #elif score <= 89

    printf("B\n");

 #elif score <= 100

    printf("A\n");

 #endif

 

 

 #define DEBUG = 1 //只需把1改成0將不顯示調試資訊

 #if DEBUG ==1 //顯示調試資訊

 #define log(format, ...) printf(format, ##__VA_ARGS__) //...表示可變參數個數,

                                                        //## 表示可以有參數也可無

 #else //不顯示調試資訊

 #define Log(format, ...)

 #endif

 

條件編譯發生在預先處理階段,在編譯之前做的事情

 核心:根據條件編譯指定的代碼

 條件不同,編譯的部分也不同,產生的目標檔案(.o)的大小也不同

 注釋不參與編譯

 

c語言機構體,枚舉,宏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.