当前位置:网站首页>sizeof、strlen求字符长度
sizeof、strlen求字符长度
2022-06-13 08:06:00 【学代码的小呆鸟】
int main()
{
//数组名表示首元素地址
//例外
//1.sizeof(数组名)-数组名表示整个数组
//2.&数组名-数组名表示整个数组
//一维数组
int a[] = {1,2,3,4};
printf("%d\n",sizeof(a));//sizeof(数组名)-计算的是数组的总大小-单位是字节-16
printf("%d\n",sizeof(a+0));//4-数组名这里表示首元素地址 a+0 还是首元素地址 地址的大小就是
4/8字节
printf("%d\n",sizeof(*a));//4-数组名表示首元素地址 *a就是首元素
printf("%d\n",sizeof(a+1));//4/8 数组名这里表示第二个元素地址 a+1
printf("%d\n",sizeof(a[1]));//4 第二个元素的大小
printf("%d\n",sizeof(&a));//4 &a取出的就是数组的地址 但是数组的地址也是地址 就是4/8
printf("%d\n",sizeof(*&a));//16-&a是数组的地址 数组的地址解引用访问的就是数组
printf("%d\n",sizeof(&a+1));//4 &a+1虽然地址跳过数组 结果还是一个地址 地址的大小就是4/8
printf("%d\n",sizeof(&a[0]));//4/8 第一个元素的地址
printf("%d\n",sizeof(&a[0]+1));//4/8 第二个元素的地址
//字符数组
char arr[] = {'a','b','c','d','e','f'};
printf("%d\n", sizeof(arr));//6 sizeof计算的是数组的大小
printf("%d\n", sizeof(arr+0));//4/8 arr是首元素的地址 arr+0还是首元素的地址 地址就是4/8
printf("%d\n", sizeof(*arr));//1 这里就是首元素的大小
printf("%d\n", sizeof(arr[1]));//这里就是第二个元素的大小
printf("%d\n", sizeof(&arr));//4/8 &arr这里还是地址 是数组的大小
printf("%d\n", sizeof(&arr+1));//还是地址 4/8 跳过这个数组后的地址
printf("%d\n", sizeof(&arr[0]+1));//还是地址 第二个元素的地址 4/8
//字符数组
char arr[] = {'a','b','c','d','e','f'};
char a[] = "abcdef";
//strlen 就找'\0' 统计到'\0'结束 但是不包括'\0'
//sizeof不一样 在""中 这里隐藏着'\0'这时候会算上'\0'
printf("%d\n", strlen(arr));//随机值
printf("%d\n", strlen(a));//6
printf("%d\n", sizeof(arr));//6
printf("%d\n", sizeof(a)); //7
printf("%d\n", strlen(arr+0));//随机值
printf("%d\n", strlen(*arr));//err
printf("%d\n", strlen(arr[1]));//err
printf("%d\n", strlen(&arr));//随机值
printf("%d\n", strlen(&arr+1));//随机值-6
printf("%d\n", strlen(&arr[0]+1));//随机值 -1
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));//7 sizeof(arr)计算的数组的大小 单位是字节
printf("%d\n", sizeof(arr+0));//4/8 计算的是地址的大小 arr+0是首元素地址
printf("%d\n", sizeof(*arr));//1 计算的是首元素的大小
printf("%d\n", sizeof(arr[1]));//2 计算的是第二个元素的大小
printf("%d\n", sizeof(&arr));//4/8 数组的地址 地址就是4/8
printf("%d\n", sizeof(&arr+1));//4/8 跳过这个数组的地址 还是个地址
printf("%d\n", sizeof(&arr[0]+1));//4/8 第二个元素的地址
char arr[] = "abcdef";
printf("%d\n", strlen(arr));//6
printf("%d\n", strlen(arr+0));//6
printf("%d\n", strlen(*arr));//err
printf("%d\n", strlen(arr[1]));//err
printf("%d\n", strlen(&arr));//6 &arr-数组的地址-数组指针-char(*p)[7]=&arr
printf("%d\n", strlen(&arr+1));//随机值
printf("%d\n", strlen(&arr[0]+1));//5
char *p = "abcdef";//这个是常量字符串 其实就是把a的地址放到了p里面去了
printf("%d\n", sizeof(p));//4/8 计算指针变量p的大小
printf("%d\n", sizeof(p+1));//4/8 p+1计算的是字符b的地址
printf("%d\n", sizeof(*p));//1 *p就是字符串的第一个字符
printf("%d\n", sizeof(p[0]));//1
printf("%d\n", sizeof(&p));//4/8
printf("%d\n", sizeof(&p+1));//4/8
printf("%d\n", sizeof(&p[0]+1));//4/8
char *p = "abcdef";
printf("%d\n", strlen(p));//6
printf("%d\n", strlen(p+1));//5
printf("%d\n", strlen(*p));//err
printf("%d\n", strlen(p[0]));//err
printf("%d\n", strlen(&p));//随机值
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 a[0]相当于第一行作为一维数组的数组名
//sizeof(arr[0])把数组名单独放在sizeof()内 计算的是第一行的大小
printf("%d\n",sizeof(a[0]+1));//4/8 a[0]是第一行的数组名 数组名此时是首元素地址
//其实就是第一行第一个元素的地址 所以arr[0]+1 就是第一行第二个元素的地址
printf("%d\n",sizeof(*(a[0]+1)));//4 是第一行第一个元素
printf("%d\n",sizeof(a+1));//4 第二行这个数组的地址 a是二维数组的数组名 没有
//sizeof(数组名) 也没有&(数组名) 所以a是首元素地址 而把二维数组看作一维数组时
//二位数组的首元素就是他的第一行 a就是第一行(首元素)的地址 a+1就是第二行的地址
printf("%d\n",sizeof(*(a+1)));//16 计算第二行的大小 sizeof(a+1) 这里a+1就是数组名
printf("%d\n",sizeof(&a[0]+1));//4 第二行的地址
printf("%d\n",sizeof(*(&a[0]+1)));//16 计算第二行的大小
printf("%d\n",sizeof(*a));//16 a是首元素地址 *a就是第一行 sizeof(*a)就是
//第一行的大小
printf("%d\n",sizeof(a[3]));//16 sizeof不会去真的访问第4行 括号里面是不参与运算的
//至于它的类型有关 所以就是和sizeof(arr[0])一个意思
return 0;
}边栏推荐
- [MySQL] rapid data deletion recovery tool - binlog2sql
- Rust writes near smart contract
- Recognition of COVID-19 based on paddlepaddle
- Data warehouse data processing and data flow
- STM32CubeMX的下载和安装方式
- 2022 simulated examination question bank and online simulated examination of hoisting machinery command examination questions
- Openharmony notes ----------- (I)
- 免费文件服务器储存技术
- Redis master-slave replication - underlying principle
- Selenium reports an error deprecationwarning: executable_ path has been deprecated, please pass in a Service object
猜你喜欢
![[redis problem] record a big key problem handling](/img/ad/787d5911fac2cc2105e439d906b67f.jpg)
[redis problem] record a big key problem handling

Tidb source code series: immersive compilation of tidb

Free file server storage technology

Altium Designer中导入和导出设置的方法

Redis Cluster - the underlying principle of cluster execution commands

Redis Cluster - the bottom principle of building clusters

18 | establish data path (middle): instruction + operation =cpu

Cosmos star application case

安装CUDA+CUSP环境,并创建第一个HelloWord入门工程

JMeter UDP pressure measurement
随机推荐
【clickhouse专栏】基础数据类型说明
Local shooting range 2- file upload vulnerability (III) - Network Security
v-for生成的子组件列表删除第n行出现数据错乱问题
Go 接口实现原理【高阶篇】: type _interface struct
19 | 建立数据通路(下):指令+运算=CPU
【PYTORCH】Expected object of type torch. xxxTensor but found type torch. cuda. xxxTensor(torch0.4.0)
ERP基础数据 华夏
Overview of cross chain protocol IBC
批发商为什么要使用订单系统
Recommend several books on DBA promotion
[introduction to flirting with girls on Valentine's day -- 63 lines of code to win]
CCNP_ Bt-ospf big experiment (1)
18 | 建立数据通路(中):指令+运算=CPU
[pytorch] pytorch0.4.0 installation tutorial and GPU configuration collection (including test code)
[MySQL] the most complete MySQL monitoring project
P7712 [Ynoi2077] hlcpq
Effective Go - The Go Programming Language
Redis master-slave replication - underlying principle
JMeter UDP pressure measurement
基于paddlepaddle的新冠肺炎识别