您的位置:首页 > 移动开发 > IOS开发

iOS开发-Day11-C的复习

2015-07-28 11:07 253 查看
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、结构体、枚举是值类型,引用类型相当于指针

此点不完善待补充
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: