当前位置:网站首页>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
边栏推荐
- Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
- Osgconv tool usage
- YOLO系列 --- xml2txt脚本
- ArrayList
- 方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
- P1596 [USACO10OCT]Lake Counting S
- Encoding and decoding of golang URL
- Some understandings of 3dfiles
- Three characteristics
- Transfinite hacker cognition
猜你喜欢

About Wireshark's unsuccessful installation of npcap
![P1596 [USACO10OCT]Lake Counting S](/img/a7/07a84c93ee476788d9443c0add808b.png)
P1596 [USACO10OCT]Lake Counting S

Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template

Ue5 opencv plug-in use

MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板

Transplantation of freetype Library
![[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

the installer has encountered an unexpected error installing this package

Haproxy+kept build 01

php-fpm软件的安装+openresty高速缓存搭建
随机推荐
L'installateur a été installé avec une erreur inattendue
I want to do large screen data visualization application feature analysis
IP production stream is so close to me
Flex flexible box layout
Unity4.3.1 engine source code compilation process
Unity2019_ Lighting system
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
Redis的数据结构
Go resolve ID card
KunlunBase MeetUP 等您来!
unity2019_ Input management
Demonstration of plug-in use of ventuz basic series
Maxcompute string splitting function -split_ PART
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
P1896 [scoi2005] non aggression (shape pressure DP)
oracle 插入单引号
Osgearth target selection
go 解析身份证
Scite change background color