指针:
一个变量,其值为另一个变量的地址,即,内存位置的直接地址。
声明:
int *ptr; /* 一个整型的指针,指针指向的类型是整型 */ double *ptr; /* 一个 double 型的指针,指针指向的类型是double*/ float *ptr; /* 一个浮点型的指针,指针指向的类型是float*/ char *ptr; /* 一个字符型的指针,指针指向的类型是char*/
赋值:
int var = 20; /*声明一个int型变量*/ int *ptr = NULL; /*声明一个int型指针,并初始化为 空 */ ptr = &var; /*将变量var的地址复制给ptr*/
数组与指针:
数组名可以看作是一个指针。
int array[10]={0,1,2,3,4,5,6,7,8,9}; int value; value=array[0]; //也可写成:value=*array; value=array[3]; //也可写成:value=*(array+3); value=array[4]; //也可写成:value=*(array+4);
指针与结构体:
->