#include "stdlib.h" #include "string.h" #include "stdio.h" // 变量本质:(一段连续)内存空间的别名,内存空间的标号 // C缺陷与陷阱 里面详细讲解了指针,深入理解指针 void main01(void) { int a; int* p = NULL; // 指针是一种数据类型 // 对应与汇编语言的直接寻址和间接寻址 //<==> int(*p) = NULL; // p-遇到*间接寻址->*p 或者 p-遇到*内容做地址->*p // 括号内是个整体*p是int类型,一个*告诉编译器需要间接寻址一次,*p 即:把p(内存空间)的内容作为地址,到该地址上再取一次值。 // 两个*就是间接寻址两次。 // 例如:int** p <==> int(**p) <==>int*(*p) // **p是int类型,*p是int*类型,p是int**类型; // p-遇到*间接寻址->*p-遇到*间接寻址->**p // 或者 // p-遇到*内容作地址->*p-遇到*内容作地址->**p //指针 左值和右值 // 指针作左值,内容作地址在该地址写值,如,*p = 20; p-遇到*内容作地址给该地址写值->*p // 指针作右值,内容作地址在该地址取值(内容作地址在该地址读值),如 b = *p; p-遇到*内容作地址取值 a = 11; // 直接修改 p = &a; *p = 20; // 指针左值:20赋值给*p, p-遇到*内容作地址在该地址写值->a int b = *p; // 指针右值:p-遇到*内容作地址在该地址取值->b printf("a:%dnb:%dn", a, b); system("pause"); } // 指针是一种数据类型,是指它指向的内存空间的数据类型 // 指针步长(p++或p--),根据所在内存空间的数据类型来确定 // 指针的步长是根据所指内存空间类型来定 // 在C++编译器角度,指针就是一个变量 int getbuf01(char* p); int getbuf01(char** p); int getbuf01(char (*p)[]); int getbuf01(char p[10][20]); int getbuf01(char****** p); // 参考文档: // https://blog.csdn.net/weixin_42586210/article/details/89187147 指针——铁律9条 void main(void) { char buf1[128] = { 0 }; char buf2[128] = { 0 }; char* p1 = buf1; char* p2 = buf2; char str[] = "abcd"; // 长度是5 'a''b''c''d'' ' 字符串以 结束 int len = sizeof(str); printf("str:%snlen:%dn", str, len); strcpy_s(buf1, len, str); printf("buf1:%sn", buf1); printf("p2:%dn", p2); //while (*p1 != ' ') { // 注意不要把' '写成" " // *p2++ = *p1++; //} // <==> while (*p2++ = *p1++); printf("p2:%dn", p2); printf("buf2:%sn", buf2); p2 = buf2; while (*p2) { // 当p2是' '时,条件为false,退出循环 printf("%c", *p2); *p2++; } // linux strcpy拷贝源代码 //char* strcpy(char* dst, const char* src) //{ // char* cp = dst; // while (*cp++ = *src++); // return(dst); //} system("pause"); }