当前位置:网站首页>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
边栏推荐
- unity2019_ Input management
- P1596 [USACO10OCT]Lake Counting S
- Kunlunbase meetup is waiting for you!
- animation
- Golang json格式和结构体相互转换
- Unity4.3.1 engine source code compilation process
- Maxcompute string splitting function -split_ PART
- Data analysis exercises
- Transfinite hacker cognition
- 【K&R】中文第二版 个人题解 Chapter1
猜你喜欢

MAE

Redis data structure

Advanced OSG collision detection

unity2019_ Input management

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

Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données

Unity2019_ Lighting system

Introduction to Base64 coding

Abstract classes and interfaces
随机推荐
Dotween plug-in
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
Basic operation and process control 2
Jupyter remote server configuration and server startup
Lua framwrok framework starts
Use of ue5 QRcode plug-in
C#课程设计之学生教务管理系统
简易入手《SOM神经网络》的本质与原理
Conversion between golang JSON format and structure
P2704 [NOI2001] 炮兵阵地(状压dp)
Golang's range
Get to know unity2 for the first time
Compilation error: "not in executable format: file format not recognized"“
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
使用base64编码传图片
Maxcompute string splitting function -split_ PART
Golang 中string和int类型相互转换
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
What is BFC?
Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!