当前位置:网站首页>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
边栏推荐
- C#课程设计之学生教务管理系统
- Image processing 8-cnn image classification
- A tunnel to all ports of the server
- [end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
- Luaframwrok handles resource updates
- Data analysis exercises
- I want to do large screen data visualization application feature analysis
- Some understandings of 3dfiles
- P1596 [USACO10OCT]Lake Counting S
- Display terrain database on osgearth ball
猜你喜欢
【更新中】微信小程序学习笔记_3
Introduction to Base64 coding
Transplantation of freetype Library
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
C course design employee information management system
Transfinite hacker cognition
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
IP production stream is so close to me
Basic operation and process control 2
Abstract classes and interfaces
随机推荐
oracle 插入单引号
Zohocrm deluge function application time verification
Golang 中string和int类型相互转换
P1596 [USACO10OCT]Lake Counting S
Are you still watching the weather forecast on TV?
About Wireshark's unsuccessful installation of npcap
Editor Extensions
C language - Introduction - essence Edition - take you into programming (I)
Cesium service deployment, and import and display local 3dfiles data
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
Exe file running window embedding QT window
796 · 开锁
My touch screen production "brief history" 2
Multi traveling salesman problem -- overview of formula and solution process
Basic operation and process control 2
E: Unable to locate package ROS melody desktop full
ArrayList
Storage of data
Unity2019_ Natural ambient light_ Sky box
What does (+) in Oracle mean