当前位置:网站首页>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
边栏推荐
猜你喜欢

jupyter远程服务器配置以及服务器开机自启

animation

unity2019_ Input management

Redis data structure

Transfinite hacker cognition

C#课程设计之学生教务管理系统
![[updating] wechat applet learning notes_ three](/img/05/958b8d62d3a42b38ca1a2d8631a7f8.png)
[updating] wechat applet learning notes_ three

數據庫應用技術課程設計之商城管理系統

Are you still watching the weather forecast on TV?

Mall management system of database application technology course design
随机推荐
Student educational administration management system of C # curriculum design
Conversion between JSON and object
P2622 关灯问题II(状态压缩 搜索)
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Unity performance optimization
Demonstration of plug-in use of ventuz basic series
Editor Extensions
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
P2622 light off problem II (state compression search)
Oracle insert single quotation mark
Cesium service deployment, and import and display local 3dfiles data
Encoding and decoding of golang URL
數據庫應用技術課程設計之商城管理系統
Youyou1 of xlua knapsack system
I want to do large screen data visualization application feature analysis
UE4 call DLL
MAE
Scite change background color
P1596 [USACO10OCT]Lake Counting S
freetype库的移植