Files
CWithClasses/C/BasicSyntax/Pointer/difference_xp4_qxpq4.cpp
2025-12-31 00:39:23 +08:00

36 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*#include <stdio.h>
int main() {
char *p[4] = {"Hello", "World", "C", "Language"};
printf("%s\n", p[1]); // 输出World
printf("%c\n", *p[1]); // 输出W p[1]指向"World"*p[1]就是'W'
printf("%c\n", *(p[1] + 1)); // 输出o p[1] + 1 是 'o' 的地址)
return 0;
}
*/
#include <stdio.h>
int main() {
int arr[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
int (*p)[4] = arr;
printf("%d\n", (*p)[2]); // 输出 3因为 (*p) 就是 arr[0],即 {1, 2, 3, 4},然后 [2] 表示访问第 3 个元素。
//1、指针表示地址 2、把指针直接看成数组
//(*p)很牛逼,什么都可以代替,什么数(组)啊,字符串(组)啊都可以。
//(*p)[i]p 是一个指向 数组(如 int[4])的指针。首先解引用 p 得到数组本身,再通过索引 [i] 访问数组中的元素。
//*p[4]:p 是一个指针组有5个指针*p[i]是解析p[i]的原值
//*(p + i)p 是一个指向某个类型元素的指针。通过指针运算 p + i 来访问偏移 i 个位置的元素。
//指针中的p[i]完全可以理解为*(p+i)
char arr[2][4] = {"abc", "def"}; // 2 个 4 字符数组
char (*p)[4] = arr; // p 指向 arr
printf("%s\n", p[0]); // 输出abc
printf("%c\n", (*p)[1]); // 输出b
p++; // p 移动到下一个 char[4] 数组
printf("%s\n", p[0]); // 输出def
return 0;
}