iOS開發-Day11-C的複習

1、函數指針
看個例子就可以明白了:

int add(int a,int b){
    return a+b;
}
int main()
{
    int (*ptr)(int, int);  //聲明一個函數指針
    int a, b;
    ptr = add;        //將指針指向函數max
    scanf("%d%d", &a, &b);
    c = (*ptr)(a,b);     //以指針形式調用函數
    printf("%d", c);
    return 0;
}

2、結構體數組,結構體指針

  • 結構體數組
    typedef struct
    {
        char *name;
        char *sex;
        int age;
    }student;

    student stu1,stu2,stu3;
    student stus={stu1,stu2,stu3};//一個結構體數組
  • 結構體指針
//接上例
    student *p;
    p=&stu1;
    printf("%s",(*p).name);//輸出stu1.name的值
    printf("%s",p->name);//與上行意義相同
  • 一個問題
typedef struct
{
    char name[20];
} student;
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        student stu1;
        //stu1.name="yc";//數組名作地址是不可以直接賦值的
        strcpy(stu1.name, "yc");
    }
    return 0;
}

3、條件編譯

  • 一般情況下,源程序中所有的行都參加編譯。但有時希望對其中一部分內容只在滿足一定條件下才進行編譯,即對一部分內容指定編譯條件,這就是“條件編譯”(conditional compile)
    例子:
    #include<stdio.h>
    #define LETTER1
    int main(int argc,char*argv[])
    {
    char str[20]="CLanguage",c;
    int i;
    i=0;
    while((c=str[i])!='\0')
    {
    i++;
    #ifdef LETTER1
    if(c>='a'&&c<='z')
    c=c-32;
    #else
    if(c>='A'&&c<='Z')
    c=c+32;
    #endif
    printf("%c",c);
    }
    return0;
    }

條件編譯有這幾種指令

#if
#else
#elif
#endif

#ifdef
#ifndef 

#error 
#line 
#pragma 

用法詳見

http://baike.baidu.com/view/1995627.htm

4、結構體、枚舉是值類型,引用類型相當於指針
此點不完善待補充

發佈了38 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章