数组和指针,sizeof和strlen

三个规则(只需要这三个规则就能判断)
  • sizeof(数组名) 和 &(数组名) 表示整体
  • 变换公式 arr[0] == *(arr+0)
  • strlen要找到\0 sizeof要计算存储大小
#include <stdio.h>
int main()
{
//一维数组
int a[] = {1, 2, 3, 4}; // 64位机器 地址为8字节
printf("%d\n", sizeof(a)); // 16 数组名是首元素地址 2个例外
printf("%d\n", sizeof(a + 0)); // 8 (单独放)sizeof数组名 和 取地址数组名 表示整个数组
printf("%d\n", sizeof(*a)); // 4
printf("%d\n", sizeof(a + 1)); // 8
printf("%d\n", sizeof(a[1])); // 4
printf("%d\n", sizeof(&a)); // 8 数组的地址
printf("%d\n", sizeof(*&a)); // 16 数组的地址解引用是数组
printf("%d\n", sizeof(&a + 1)); // 8
printf("%d\n", sizeof(&a[0])); // 8
printf("%d\n", sizeof(&a[0] + 1)); // 8
//字符数组
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
printf("%d\n", sizeof(arr)); // 6
printf("%d\n", sizeof(arr + 0)); // 8 首元素的地址+0=首元素的地址
printf("%d\n", sizeof(*arr)); // 1 首元素地址解引用=首元素
printf("%d\n", sizeof(arr[1])); // 1
printf("%d\n", sizeof(&arr)); // 8
printf("%d\n", sizeof(&arr + 1)); // 8
printf("%d\n", sizeof(&arr[0] + 1)); // 8
printf("%d\n", strlen(arr)); // 随机值 数组后面放啥不清楚 找到\0为止
printf("%d\n", strlen(arr + 0)); // 随机值
//printf("%d\n", strlen(*arr)); // strlen传进去的是地址 传了‘a’为97 报错
//printf("%d\n", strlen(arr[1])); // 地址为98 报错
printf("%d\n", strlen(&arr)); // 随机值
printf("%d\n", strlen(&arr+1)); // 随机值-6
printf("%d\n", strlen(&arr[0]+1)); // 随机值-1

char arr[] = "abcdef"; //7个元素的数组
printf("%d\n", sizeof(arr)); //7 计算的是数组的大小6+\0
printf("%d\n", sizeof(arr+0)); //8 首元素的地址
printf("%d\n", sizeof(*arr)); //1
printf("%d\n", sizeof(arr[1])); //1
printf("%d\n", sizeof(&arr)); //8
printf("%d\n", sizeof(&arr+1)); //8
printf("%d\n", sizeof(&arr[0]+1)); //8
printf("%d\n", strlen(arr)); //6
printf("%d\n", strlen(arr+0)); //6
printf("%d\n", strlen(*arr)); //报错
printf("%d\n", strlen(arr[1])); //报错
printf("%d\n", strlen(&arr)); //6
printf("%d\n", strlen(&arr+1)); //随机值
printf("%d\n", strlen(&arr[0]+1)); //5
char *p = "abcdef"; //把常量字符串a的地址放到p里去
printf("%d\n", sizeof(p)); //8 p就是地址
printf("%d\n", sizeof(p+1)); //8
printf("%d\n", sizeof(*p)); //1
printf("%d\n", sizeof(p[0])); //1 等价 arr[0] == *(p+0)
printf("%d\n", sizeof(&p)); //8
printf("%d\n", sizeof(&p+1)); //8
printf("%d\n", sizeof(&p[0]+1)); //8
printf("%d\n", strlen(p)); //6
printf("%d\n", strlen(p+1)); //5
printf("%d\n", strlen(*p)); //报错
printf("%d\n", strlen(p[0])); //报错
printf("%d\n", strlen(&p)); //随机值 p里面存放的是常量地址 地址里有无0不确定
printf("%d\n", strlen(&p+1)); //随机值
printf("%d\n", strlen(&p[0]+1)); //5 第二个元素的地址
//二维数组
int a[3][4] = {0};
printf("%d\n",sizeof(a)); //48
printf("%d\n",sizeof(a[0][0])); //4
printf("%d\n",sizeof(a[0])); //16 首元素地址
printf("%d\n",sizeof(a[0]+1)); //4 == *(a+0)+1 第一行第一个元素地址
printf("%d\n",sizeof(*(a[0]+1))); //4 第一行第二个元素
printf("%d\n",sizeof(a+1)); //8 第二行的地址
printf("%d\n",sizeof(*(a+1))); //16 ==a[1]
printf("%d\n",sizeof(&a[0]+1)); //8 第二行的地址
printf("%d\n",sizeof(*(&a[0]+1))); //16 第二行的元素
printf("%d\n",sizeof(*a)); //16 第一行的元素
printf("%d\n",sizeof(a[3])); //16 == *(a+3)

}
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int *ptr = (int *)(&a + 1); //强制类型转换使得指针的加减法权重改变
printf("%d,%d", *(a + 1), *(ptr - 1)); //2 5
return 0;
}
struct Test
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
} * p;
//已知,结构体Test类型的变量大小是20个字节
int main()
{
p = (struct Test*)0x100000;
printf("%p\n", p + 0x1); //0x100014
printf("%p\n", (unsigned long)p + 0x1); //单纯地整型加1
//0x100001
printf("%p\n", (unsigned int *)p + 0x1); //0x100004
return 0;
}
#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int *ptr1 = (int *)(&a + 1);
int *ptr2 = (int *)((int)a + 1); //地址+1个字节
printf("%x,%x", ptr1[-1], *ptr2);
//4 //2000000
return 0;
}
#include <stdio.h>
int main()
{
int a[3][2] = {(0, 1), (2, 3), (4, 5)}; //这是逗号表达式
int *p; //a[3][2]={1,3,5}
p = a[0];
printf("%d", p[0]);
return 0;
}
int main()
{
int a[5][5];
int(*p)[4];
p = a; //int(*)[5] 和 int(*)[4]
printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
// -4 的补码 直接作为地址 地址的加减法表示元素的个数
return 0;
}
int main()
{
int aa[2][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr1 = (int *)(&aa + 1);
int *ptr2 = (int *)(*(aa + 1));
printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1)); //10 5
return 0;
}
#include <stdio.h>
int main()
{
char *a[] = {"work", "at", "alibaba"}; //数组
char **pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
int main()
{
char *c[] = {"ENTER", "NEW", "POINT", "FIRST"}; //数组
char **cp[] = {c + 3, c + 2, c + 1, c};
char ***cpp = cp;
printf("%s\n", **++cpp); //point
printf("%s\n", *--*++cpp + 3); //ER
printf("%s\n", *cpp[-2] + 3); // == **(cpp-2)+3 ST
printf("%s\n", cpp[-1][-1] + 1); //*(*(CPP-1)-1) EW
return 0;
}
//(cpp-2) 不会改变cpp的值

[C语言] 各种疑难杂症_#include