当前位置:网站首页>Explain sizeof, strlen, pointer, array and other combination questions in detail
Explain sizeof, strlen, pointer, array and other combination questions in detail
2022-07-03 08:21:00 【On the Pearl River】
This part belongs to C Advanced language play
Catalog
This part belongs to pointer 、 Array 、sizeof and strlen Advanced gameplay combined , Help you win easily !
1. Summary of pit points
2. title
Summary of pit points
1. sizeof
①sizeof( Array name ); The array name here represents the entire array , Instead of the address of the entire array
②sizeof It's the operator , Calculate the memory space occupied by the type of variable , Unit is byte , Don't pay attention to the contents stored in memory
③sizeof Is based on type analysis , Will not access specific content
④printf("%d\n",sizeof( The pointer )), The size obtained is 4 Bytes ( stay x86 In the environment ) or 8 Bytes ( stay x64 In the environment )
2. strlen
①strlen It's a library function , When using, you need a header file <string.h>, Specifically for string length , Only for Strings , Keep looking back \0, Statistics \0 The number of characters before
②strlen() You can only receive addresses ,strlen('a') or strlen(97), Illegal access , It's a wild pointer !
title
The following topics cover Understanding of logarithmic group names 、 The operation of pointer and the meaning of pointer type And other very important knowledge points ,
// One dimensional array
Key summary :
①sizeof() Only pay attention to the types in brackets , Don't pay attention to its content
②sizeof( The pointer ) The answer is 4 Bytes or 8 Bytes
int a[ ] = {1,2,3,4};
printf("%d\n",sizeof(a)); have to 16
printf("%d\n",sizeof(a+0)); have to 4 or 8
printf("%d\n",sizeof(*a)); have to 4
printf("%d\n",sizeof(a+1)); have to 4 or 8
printf("%d\n",sizeof(a[1])); have to 4
printf("%d\n",sizeof(&a)); have to 4 or 8
printf("%d\n",sizeof(*&a)); have to 16
printf("%d\n",sizeof(&a+1)); have to 4 or 8
printf("%d\n",sizeof(&a[0])); have to 4 or 8
printf("%d\n",sizeof(&a[0]+1)); have to 4 or 8
// A character array
Key summary :
①strlen() Only address can be stored in brackets , And follow this address all the way back \0
② The character pointer stores the address of the first character , It is not the same address as the address of this pointer
char arr[ ] = {'a','b','c','d','e','f'};
printf("%d\n", sizeof(arr)); have to 6
printf("%d\n", sizeof(arr+0)); have to 4 or 8
printf("%d\n", sizeof(*arr)); have to 1
printf("%d\n", sizeof(arr[1])); have to 1
printf("%d\n", sizeof(&arr)); have to 4 or 8
printf("%d\n", sizeof(&arr+1)); have to 4 or 8
printf("%d\n", sizeof(&arr[0]+1)); have to 4 or 8
printf("%d\n", strlen(arr)); Get a random value
printf("%d\n", strlen(arr+0)); Get a random value
printf("%d\n", strlen(*arr)); have to error
printf("%d\n", strlen(arr[1])); have to error
printf("%d\n", strlen(&arr)); Get a random value
printf("%d\n", strlen(&arr+1)); Get a random value -6
printf("%d\n", strlen(&arr[0]+1)); Get a random value -1
char arr[ ] = "abcdef";
printf("%d\n", sizeof(arr)); have to 7
printf("%d\n", sizeof(arr+0)); have to 4 or 8
printf("%d\n", sizeof(*arr)); have to 1
printf("%d\n", sizeof(arr[1])); have to 1
printf("%d\n", sizeof(&arr)); have to 4 or 8
printf("%d\n", sizeof(&arr+1)); have to 4 or 8
printf("%d\n", sizeof(&arr[0]+1)); have to 4 or 8
printf("%d\n", strlen(arr)); have to 6
printf("%d\n", strlen(arr+0)); have to 6
printf("%d\n", strlen(*arr)); have to error
printf("%d\n", strlen(arr[1])); have to error
printf("%d\n", strlen(&arr)); have to 6
printf("%d\n", strlen(&arr+1)); Get a random value
printf("%d\n", strlen(&arr[0]+1)); have to 5
char *p = "abcdef";
printf("%d\n", sizeof(p)); have to 4 or 8
printf("%d\n", sizeof(p+1)); have to 4 or 8
printf("%d\n", sizeof(*p)); have to 1
printf("%d\n", sizeof(p[0])); have to 1
printf("%d\n", sizeof(&p)); have to 4 or 8
printf("%d\n", sizeof(&p+1)); have to 4 or 8
printf("%d\n", sizeof(&p[0]+1)); have to 4 or 8
printf("%d\n", strlen(p)); have to 6
printf("%d\n", strlen(p+1)); have to 5
printf("%d\n", strlen(*p)); have to error
printf("%d\n", strlen(p[0])); have to error
printf("%d\n", strlen(&p)); Get a random value
printf("%d\n", strlen(&p+1)); Get a random value
printf("%d\n", strlen(&p[0]+1)); have to 5
// Two dimensional array
Key summary :
① A two-dimensional array arr[3][4],arr[0] Represents the address of the first line ,arr[1] Indicates the address of the second line ,arr[2] Indicates the address of the third line
②sizeof('a'+1), Integer promotion will occur

int a[3][4] = {0};
printf("%d\n",sizeof(a)); have to 48
printf("%d\n",sizeof(a[0][0])); have to 4
printf("%d\n",sizeof(a[0])); have to 16
printf("%d\n",sizeof(a[0]+1)); have to 4 or 8
Particular attention : In this question a[0] Represents the address of the first element in the first line ,+1 Is the address of the second element in the first line
printf("%d\n",sizeof(*(a[0]+1))); have to 4
printf("%d\n",sizeof(a+1)); have to 4 or 8
Particular attention : In this question a Represents the address of the first row of the two-dimensional array ,+1 Is the address of the second row of the two-dimensional array
printf("%d\n",sizeof(*(a+1))); have to 16
printf("%d\n",sizeof(&a[0]+1)); have to 4 or 8
Particular attention : In this question &a[0] Represents the address of the first row of the two-dimensional array ,+1 Is the address of the second row of the two-dimensional array
printf("%d\n",sizeof(*(&a[0]+1))); have to 16
printf("%d\n",sizeof(*a)); have to 16
Particular attention : In this question a Represents the address of the first line of elements , The first row of the two-dimensional array after dereference
printf("%d\n",sizeof(a[3])); have to 16
Particular attention : Even though a[3] It belongs to cross-border visit , however sizeof Calculate the memory space occupied by the type of variable ,sizeof Analysis by type , The fourth line will not be accessed
Conclusion
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
边栏推荐
- Mysql容器化(1)Docker安装MySQL
- What is BFC?
- Golang string segmentation, substitution and interception
- Golang json格式和结构体相互转换
- IP production stream is so close to me
- Base64 and base64url
- Redis data structure
- the installer has encountered an unexpected error installing this package
- [audio and video] ijkplayer error code
- go 解析身份证
猜你喜欢

Basic operation and process control

Jupyter remote server configuration and server startup

freetype库的移植

MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
![[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel](/img/51/04f5a9dbd03438fbdf25545a81b7ba.jpg)
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel

C course design employee information management system

Viz artist advanced script video tutorial -- stringmap use and vertex operation
![[cloud native] introduction and use of feign of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] introduction and use of feign of microservices

Wpf: solve the problem that materialdesign:dialoghost cannot be closed

数据分析练习题
随机推荐
Generate video using clipout in viz engine
超限黑客认知
Are you still watching the weather forecast on TV?
Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!
【音视频】ijkplayer错误码
Introduction to hexadecimal coding
E: Unable to locate package ROS melody desktop full
Luaframwrok handles resource updates
Youyou1 of xlua knapsack system
Abstract classes and interfaces
Advanced OSG collision detection
The general trend of data news releases the power of visual reporting ----- essays after reading
C#课程设计之学生教务管理系统
P2704 [NOI2001] 炮兵阵地(状压dp)
L'installateur a été installé avec une erreur inattendue
Use of ue5 QRcode plug-in
Base64和Base64URL
Golang json格式和结构体相互转换
Cesium service deployment, and import and display local 3dfiles data
一条通往服务器所有端口的隧道