这篇文档记录用更简短更清晰的示例来理解指针。
#include <stdio.h>
int main() {
int a = 50; //定义普通变量a
int* aPtr = &a; //定义指针变量aPtr,下面统一称之为变量,
int** bPtr = &aPtr; //定义指向指针变量的指针变量bPtr,下面统一称之为变量
//方式一:用打印地址和地址值的方式作推导,来理解指针以及指针的指针的概念
printf("variable a value = %d\n",a); //打印变量a的值
printf("variable a address = %p\n",&a);//打印变量a的地址
printf("\n");
printf("variable aPtr value = %p\n",aPtr);//打印变量aPtr的值
printf("variable aPtr address = %p\n",&aPtr);//打印变量aPtr的地址
printf("variable aPtr -> value = %d\n",*aPtr);//把变量aPtr的值作为地址,打印地址上存储的值,即a的值
printf("\n");
printf("variable bPtr value = %p\n",bPtr);//打印变量bPtr的值
printf("variable bPtr address = %p\n",&bPtr);//打印变量bPtr的地址
printf("variable bPtr -> value = %p\n",*bPtr);//把变量bPtr的值作为地址,打印地址上存储的值,即*&aPtr = aPtr = &a的的值
printf("\n");
printf("variable bPtr -> value = %d\n",**bPtr);//把*bPtr作为地址,打印地址上存储的值,即*(*&aPtr) = *(aPtr) = *(&a) = a的值
//方式二:用变量共性作推导,来理解指针以及指针的指针的概念
//int a = 50; //定义普通变量a,a = 50
//int* aPtr = &a; //定义指针变量aPtr,下面统一称之为变量,参照变量a = 50,aPtr = &a,*aPtr = *(&a) = a
//int** bPtr = &aPtr; //定义指向指针变量的指针变量bPtr,下面统一称之为变量,
//参照变量a = 50,bPtr = &aPtr,*bPtr = *(&aPtr) = aPtr = &a,**bPtr = *(&a) = a
}
variable a value = 50
variable a address = 0x7ffee5660b5c
variable aPtr value = 0x7ffee5660b5c
variable aPtr address = 0x7ffee5660b50
variable aPtr -> value = 50
variable bPtr value = 0x7ffee5660b50
variable bPtr address = 0x7ffee5660b48
variable bPtr -> value = 0x7ffee5660b5c
variable bPtr -> value = 50
网友评论