目录
指针
1、指针所占内存大小
数据的地址和数据本身没有任何关系,由于指针是间接寻址,所以猜测所有类型的指针大小相同
指针大小是由当前CPU运行模式的寻址位数决定,是固定的。
证实如下:
pointer.c
#include<stdio.h>
#include<stdbool.h>//在C99标准里面,定义了bool类型变量。引入头文件 <stdbool.h>,就能在C语言里面正常使用bool类型
int main(){
char* char_ptr=NULL;
int* int_ptr=NULL;
float* float_ptr=NULL;
double* double_ptr=NULL;
void* void_ptr=NULL;
bool* bool_ptr=NULL;
printf("The size of char_ptr is %d\n",sizeof(char_ptr));
printf("The size of int_ptr is %d\n",sizeof(int_ptr));
printf("The size of float_ptr is %d\n",sizeof(float_ptr));
printf("The size of double_ptr is %d\n",sizeof(double_ptr));
printf("The size of void_ptr is %d\n",sizeof(void_ptr));
printf("The size of bool_ptr is %d\n",sizeof(bool_ptr));
}
运行结果:
The size of char_ptr is 4
The size of int_ptr is 4
The size of float_ptr is 4
The size of double_ptr is 4
The size of void_ptr is 4
The size of bool_ptr is 4
2、*
和 &
*
:间接访问
&
:取地址
例子:
#include<stdio.h>
int main(){
int *a=NULL;
int b=10;
printf("The address of a is %d\n",&a);
printf("The address of b is %d\n",&b);
a=&b;
printf("The value of a is %d\n",a);
*a=100;
printf("The value of b is %d\n",b);
}
运行结果
The address of a is 6422268
The address of b is 6422264
The value of a is 6422264
The value of b is 100
解释:
总结:
当*a做左值时,表示地址值为a的变量
当*a做右值时,表示地址值为a的变量的值
常见错误:
#include<stdio.h>
int main(){
int *a=NULL;//int *a;
*a=10;
}
第一行代码定义了一个指针变量a,并为其赋值为NULL
第二行代码将10赋值给一个地址为NULL的变量(没有意义)
如果一开始没有给a赋值为NULL,*a=10,把10存储在一个地址不知道等于多少的内存位置。
3、 ++
自增与--
自减与*
指针间接访问
自减与自增同理,这里只记录自增
cp++与++cp
cp++
#include<stdio.h>
int main(){
char ch='a';
char *cp=&ch;
printf("The value of cp is %d\n",cp);
char *c=cp++;
printf("The value of c is %d\n",c);
printf("The value of cp is %d\n",cp);
}
运行结果:
The value of cp is 6422263
The value of c is 6422263
The value of cp is 6422264
++cp
#include<stdio.h>
int main(){
char ch='a';
char *cp=&ch;
printf("The value of cp is %d\n",cp);
char *c=++cp;
printf("The value of c is %d\n",c);
printf("The value of cp is %d\n",cp);
}
运行结果:
The value of cp is 6422263
The value of c is 6422264
The value of cp is 6422264
解释:
*cp++与 *++cp
结合方向从右往左
*cp++
步骤一:先执行cp++(结果如上)
步骤二:间接访问cp++(cp++返回的是cp自增之前的值,也就是ch的地址)
所以*cp++做左值的时候就是变量ch
*cp++做右值的时候就是ch的值’a’
注意:此时cp的值已经加一了,该表达式常常在循环中出现,为数组赋值
*cp++做左值时:
#include<stdio.h>
int main(){
char ch='a';
char *cp=&ch;
*cp++='b';//做左值时
printf("The value of ch is %c\n",ch);
}
运行结果:
The value of ch is b
*cp++做右值时:
#include<stdio.h>
int main(){
char ch='a';
char *cp=&ch;
char c=*cp++;//做右值时
printf("The value of c is %c\n",c);
}
运行结果:
The value of c is a
*++cp
步骤一:先执行++cp(结果如上)
步骤二:间接访问++cp(++cp返回的是cp的值加一,也就是ch的地址加一)
此时*cp++无法做左值,这个地址未清晰定义
*cp++做右值的时候无法确定结果
++*cp与(*cp)++
++*cp
步骤一:先执行*cp,做左值时也就是变量ch,做右值时为值’a’
步骤二:返回一个(*cp)加一的拷贝,由于这个返回的值未清晰定义,所以不能左值,做右值时为’a’+1,也就是b
++*cp做右值时
#include<stdio.h>
int main(){
char ch='a';
char *cp=&ch;
char c=++*cp;//做右值时
printf("The value of c is %c\n",c);
}
运行结果
The value of c is b
(*cp)++
步骤一:先执行*cp,做左值时也就是变量ch,做右值时为值’a’
步骤二:返回一个(*cp)的拷贝,由于这个返回的值未清晰定义,所以不能左值,做右值时为’a’
注意此时的ch已经加一了,值为’b’
(*cp)++做右值时:
#include<stdio.h>
int main(){
char ch='a';
char *cp=&ch;
char c=(*cp)++;//做右值时
printf("The value of c is %c\n",c);
printf("The value of ch is %c\n",ch);
}
运行结果
The value of c is a
The value of ch is b
总结:
自增运算符与指针中的间接取值符结合按从右往左的顺序
++cp表示先加一,再返回拷贝的值
cp++表示先返回拷贝的值,再加一
4、指针的指针
例子
#include<stdio.h>
int main(){
int a=100;
int *b=&a;
int **c=&b;
printf("%d,%d,%d\n",&a,&b,&c);
printf("The value of a is %d\n",a);
printf("The value of b is %d\n",b);
printf("The value of *b is %d\n",*b);
printf("The value of c is %d\n",c);
printf("The value of *c is %d\n",*c);
printf("The value of **c is %d\n",**c);
}
运行结果
6422268,6422264,6422260
The value of a is 100
The value of b is 6422268
The value of *b is 100
The value of c is 6422264
The value of *c is 6422268
The value of **c is 100
表达式 | 等效的表达式 |
---|---|
a | 100 |
b | &a; 6422268 |
*b | a; 100 |
c | &b; 6422264 |
*c | b; &a; 6422268 |
**c | a; 100; *b |
解释:
5、c语言中指针与其他结合使用
1、堆–动态分配
#include<stdio.h>
#include<stdlib.h>
int main(void){
int *var_ptr=(int *)malloc(sizeof(int));
*var_ptr=4;
printf("%d\n",*var_ptr);
free(var_ptr);//释放
var_ptr=NULL;
}
2、循环–为数组赋值
#include<stdio.h>
#include<stdlib.h>
int main(void){
int a[10];
int *var_ptr=a;
int i,j;
for (i = 0; i < 10; i++)
{
*var_ptr++=i;
}
for (j = 0; j < 10; j++)
{
printf("%d\n",a[j]);
}
}
3、结构体
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int int_filed;
double dbl_field;
}my_struct_type;
int main(void){
my_struct_type *s;
s=(my_struct_type*)malloc(sizeof(my_struct_type));
s->int_filed=0;
s->dbl_field=0.0;
(*s).int_filed=0; //另一种赋值的方式
printf("%d\n",s->int_filed);
printf("%ld\n",s->dbl_field);
free(s);
s=NULL;
}