一:sizeof与strlen的语法
sizeof 是计算对象或者类型创建的对象所占内存空间的大小,单位是字节
sizeof 是操作符,不是函数
strlen 是库函数
strlen 求字符串长度的,计算的是字符串中\0之前出现的字符的个数
统计到\0为止,如果没有看到\0,会继续往后找。
二:sizeof与strlen的常见笔试题
补充:
关于数组名(arr)
数组名是数组首元素的地址
但是有2个例外:
- sizeof(数组名) - 数组名表示整个数组,计算的是整个数组的大小,单位是字节
- &数组名 - 数组名也表示整个数组,取出的是整个数组的地址
除了这个2个例外,你见到的所有的数组名都表示首元素的地址。
例题一:
#include<stdio.h>
int main()
{
//一维数组
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a + 0));
printf("%d\n", sizeof(*a));
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(a[1]));
printf("%d\n", sizeof(&a));
printf("%d\n", sizeof(*&a));
printf("%d\n", sizeof(&a + 1));
printf("%d\n", sizeof(&a[0]));
printf("%d\n", sizeof(&a[0] + 1));
return 0;
}
例题二:
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = { 'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr + 0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr + 1));
printf("%d\n", sizeof(&arr[0] + 1));
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr + 0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));
printf("%d\n", strlen(&arr[0] + 1));
return 0;
}
例题三:
char arr[] = "abcdef";
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr + 0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));
printf("%d\n", strlen(&arr[0] + 1));
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr + 0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr + 1));
printf("%d\n", sizeof(&arr[0] + 1));
例题四
int main()
{
const char* p = "abcdef";
printf("%zu\n", sizeof(p));
printf("%zu\n", sizeof(p + 1));
printf("%zu\n", sizeof(*p));
printf("%zu\n", sizeof(p[0]));
printf("%zu\n", sizeof(&p));
printf("%zu\n", sizeof(&p + 1));
printf("%zu\n", sizeof(&p[0] + 1));
printf("%d\n", strlen(p));
printf("%d\n", strlen(p + 1));
printf("%d\n", strlen(*p));
printf("%d\n", strlen(p[0]));
printf("%d\n", strlen(&p));
printf("%d\n", strlen(&p + 1));
printf("%d\n", strlen(&p[0] + 1));
return 0;
}
例题五
#include<stdio.h>
#include<string.h>
int main()
{
int a[3][4] = { 0 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a[0][0]));
printf("%d\n", sizeof(a[0]));
printf("%d\n", sizeof(a[0] + 1));
printf("%d\n", sizeof(*(a[0] + 1)));
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(*(a + 1)));
printf("%d\n", sizeof(&a[0] + 1));
printf("%d\n", sizeof(*(&a[0] + 1)));
printf("%d\n", sizeof(*a));
printf("%d\n", sizeof(a[3]));
return 0;
}
例题六
//由于还没学习结构体,这里告知结构体的大小是20个字节
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
//假设p的值为0x100000.如下表表达式的值分别为多少?
//已知结构体Test类型的变量大小是20个字节
int main()
{
p = (struct Test*)0x100000;
//0x1表示16进制的1,指针+-整数,跳过1个结构体数据类型
printf("%p\n", p + 0x1);//0x100014
//将p(结构体指针类型)强行转化成为unsigned long整型,整型+1就是单纯的加减法运算
printf("%p\n", (unsigned long)p + 0x1);//0x100001
//将p(结构体指针类型)转化为了unsigned int*类型,因此+1跳过了4个字节
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);
printf("%x,%x", ptr1[-1], *ptr2);
return 0;
}
例题八
#include<stdio.h>
int main()
{
//数组的初始化内容有逗号表达式,实际上数组初始化的是1,3,5
int a[3][2] = { (0, 1), (2, 3), (4, 5) };
//1 3
//5 0
//0 0
//int a[3][2] = {{0,1}, {2,3}, {4,5}};和上面的不一样
//0 1
//2 3
//4 5
int* p;
p = a[0];
printf("%d", p[0]);//1
return 0;
}
例题九
#include<stdio.h>
int main()
{
int a[5][5];
int(*p)[4];
p = a;
printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
return 0;
}
例题十
#include<stdio.h>
int main()
{
int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* ptr1 = (int*)(&aa + 1);//表示跳过了整个数组,指向10的右边
int* ptr2 = (int*)(*(aa + 1));//aa+1表示第二行的地址,解引用后为6
printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));//10 5
return 0;
}
例题十一
#include<stdio.h>
int main()
{
char* a[] = { "work","at","alibaba" };
char** pa = a;//可以理解成char* *pa
pa++;
printf("%s\n", *pa);//at
return 0;
}