第一章 指针
知识点1.
a. 函数内的局部变量存在了STACK,全球变量存在了GLOBALS。
b. 指针和变量一样,内容是地址
c. *variable = address
d. &address = variable
e. *也作为定义指针内容用
例程1.
void gosoutheast(int* lat, int* lon)//定义了指针lat lon
{
*lat=*lat+1;//指针lat下的值+1
lon=lon+1;//指针lon地址+1
}
int main()
{
int latitude;
int longitude;
gosoutheast(&latitude,&longitude);//变量latitude
longitude的地址
}
***************************************************************************
知识点2.
a. 数组可以作为指针,指向数列第一个元素。
b. 在函数内定义的数列会被视为指针。
c. 数组作为指针只分配地址,不存数组变量,所以数组指针不能指向别的地址
例程2.
int drinks[]={1,2,3};
drinks[2] == *(drinks +2);
*****************************************************************************
知识点3.
a. 申明阶段出现*代表变量是指针。
b. string存于只读空间,需要复制到新的数组才能改变。
例程3.
char *cards = "string";//变量指向string,string不能改变
char cards[] = "string";//数组string,string可以改变
********************************************************************************
知识点4.
a. 函数名是函数的指针,地址储存在constants里。使用函数时是在访问函数的地址。
b. return type(*Pointer variable)(Param types)
char**(*names_fn)(char*,int)
c. void(*replies[])(response) = {Func1,Func2,Func3};
例程4.1
int sports_no_bieber(char *s)
{
...
}
int sports_or_workout(char *s)
{
...
}
void find( int(*match)(char*) )
{
a = match(ADS[i]);
}
int main()
{
find(sports_no_bieber);
find(sports_or_workout);
}
例程4.2
enum response_type{DUMP, SECOND_CHANCE, MARRIAGE}; //set
symbols
typedef struct{
char *name;
enum response_type type;
}response; //set
structure
void dump(response r) //set functions action
{
...
r.name
}
void second_chance(response r)
{
...
r.name
}
void (*replies[])(response) = {dump, second_chance};
//make the function array with
according array paramters
int main()
{
response r[] = ({"M",DUMP},{"L",SECOND_CHANCE}};
//list variables in strucutre
for(int i=0;i<4;i++) //different case
different functions same parameters
{
(replies[r[i].type])(r[i]); //执行函数数组
}
}
***********************************************************************
第二章 结构体
知识点1.
a. struct是数据类型,综合了其它数据类型。
b. 用typedef struct就不需要名字。
例程1.
typedef struct{
float capacity;
int psi;
const char *material;//变量为指针表示值不会变
}equipment; //数据类型叫equipment
typedef struct{
const char *name;
equipment kit; //equipment类型的名称叫做kit
}driver;
void badge(driver d) //driver类型的名称叫做d
{
d.name,d.kit.psi;
}
int main()
{
diver randy = {"Randy",{5,3,"Neo"}};
badge(randy);
}
***********************************************************************
知识点2.
a. 指针指向结构体
例程2.
void happy_birthday(turtle *t)
{
t->age = t->age +1;//t->age == (*t.age)
}
happy_birthday(&myturtle);
***********************************************************************
知识点3.
union选择最大的数据类型长度最为存储空间,并且共用此空间。
{}大括号加数字表示访问union里的第一个数。
例程3.
typedef union{
float lemon;
int lime_pieces;
}lemon_lime;
typedef struct{
float tequila;
float cointreau;
lemon_lime citrus;
}margatira;
margarita m = {2,1,{.lime_pieces=1}};
********************************************************************
知识点4.
a. enum存储符号
例程4.
typedef enum{
COUNT,POUNDS,PINT
}unit_of_measure;
typedef union{
float weight;
float volume;
}quantity;
typedef struct{
const char *name;
const char *country;
quantity amount;
unit_of_measure units;
}fruit_order;
void display(fuit_order order)
{
if(order.units == PINTS) printf
(%f,order.amount.volume);
if(order.units == POUNDS) printf
(%f,order.amount.weight);
}