C语言编程之指针

在32bit系统中,指针就4个字节

指针指向内存空间,一定要保证合法性(段错误)


修饰符:const、voliatile、typedef


const:

const char *p                   只读(指向地址可变,指向地址的内容只读)

char * const p                  指向固定地址,但指向地址的内容可变(硬件资源关系大)

const char * const p         地址内容均不可变

char *p = "Hello world"      //“”代表常量,强行修改会出现段错误

volatile:防止优化指向内存的地址

   volatile char *p;

    *p =0x10;

     while(*p ==0x10);

        xxx


指针的加减法运算,实际上加的是一个单位,单位大小:sizeof(指针的属性,如char、int等)

int *p =xxx;     //p=0x12  

p++;               //   p==0x12+1*sizeof(*p)      

变量名 [n]:        n:ID便签       地址内容的标签访问方式

               p[2] 相当于p+2

#include<stdio.h>
int main(){
	int a = 0x12345678 ;
	int b = 0x77225577 ;
	int *p1 = &b;
	char *p2 = (char *)&b;

	printf("The p1+1 is %x,%x,%x\n", *(p1+1),p1[1],*p1+1);
	printf("the p2+1 is %x\n",p2[1]);
	return 0;
}

运行结果 

ubuntu@ubuntu-virtual-machine:~$ ./pointer
The p1+1 is 12345678,12345678,77225578
the p2+1 is 55

注:内存分配自高向低地址,而读内存是自低向高

#include<stdio.h>
int main(){
	const int a = 0x12345678 ;
	int b = 0x77225577 ;
	int *p1 = &b;
	char *p2 = (char *)&b;
	p1[1] = 0x100;
	printf("The p1+1 is %x,%x,%x\n", *(p1+1),p1[1],*p1+1);
	printf("the p2+1 is %x\n",p2[1]);
	return 0;
}
//运行结果

ubuntu@ubuntu-virtual-machine:~$ ./pointer
The p1+1 is 100,100,77225578
the p2+1 is 55

逻辑操作符:>=  <=   ==   !=

最常用:==   !=

              1、跟一个特殊值进行比较   0x0:地址的无效值,结束标志

                     if(p== 0x0)  或 if(p ==NULL)

              2、指针必须是同类型的比较才有意义


多级指针:

                    

猜你喜欢

转载自blog.csdn.net/weixin_42039602/article/details/82261767