当前位置:网站首页>C language pointer classic interview question - the first bullet
C language pointer classic interview question - the first bullet
2022-07-04 09:38:00 【Hair is not as much as code】
Catalog
sizeof() and char Type one-dimensional array
strlen and char Type one-dimensional array
Character pointer and sizeof()
Character pointer and strlen()
Two dimensional arrays and sizeof()
#include<stdio.h>
int main()
{
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a));
// The result is 16, Here is the whole array
printf("%d\n", sizeof(a + 0));
// The result is 4 or 8, Here is the size of the first address
printf("%d\n", sizeof(*a));
// The result is 4, Here is the size of the first element
printf("%d\n", sizeof(a + 1));
// The result is 4 or 8, Here is the size of the second address
printf("%d\n", sizeof(a[1]));
// The result is 4, Here is the size of the second element
printf("%d\n", sizeof(&a));
// The result is 4 or 8, Here is the address size of the entire array
printf("%d\n", sizeof(*&a));
// The result is 16, Here we first take out the entire array address , Then dereference the entire array , That is, the size of the entire array is calculated
printf("%d\n", sizeof(&a + 1));
// The result is 4 or 8, The entire array is skipped here , Calculate the address size after the array , So it is 4 or 8,&a The type is int(*)[4]
printf("%d\n", sizeof(&a[0]));
// The result is 4 or 8, Here is the address size of the first element
printf("%d\n", sizeof(&a[0] + 1));
// The result is 4 or 8, Here is the address size of the second element
return 0;
}
Be careful : here &a The type is int(*)[4], Array pointer ,
&a+1 Is to skip the entire array
sizeof() and char Type one-dimensional array
char arr[] = { 'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));
//6, Here we calculate the whole array
printf("%d\n", sizeof(arr + 0));
//4 or 8, Here we calculate the address of the first element
printf("%d\n", sizeof(*arr));
//1, Here we calculate the size of the first element
printf("%d\n", sizeof(arr[1]));
//1, Philosophy calculates the size of the second element
printf("%d\n", sizeof(&arr));
//4 or 8, Here we calculate the address size of the whole array
printf("%d\n", sizeof(&arr + 1));
// still 4 or 8, The calculation here is after skipping the entire array , The size of the entire address
printf("%d\n", sizeof(&arr[0] + 1));
// still 4 or 8, Here we calculate the address size of the second element
strlen and char Type one-dimensional array
char arr[] = { 'a','b','c','d','e','f' };
printf("%d\n", strlen(arr));
// Random value , Here we calculate the whole array , That is, calculate from the first element to find \0 Location , The random value is because there is no \0
printf("%d\n", strlen(arr + 0));
// Random value , here strlen Is counting back from the first element , The value of random value is the same as the above value
printf("%d\n", strlen(*arr));
// Report errors , Here is the first element , The first element is a value ,strlen() What is in brackets must be char * Pointer variable of type , And the elements in it are char The type is not char * type , If the 'a' Transfer the past , It is equivalent to 97 As a pointer through
printf("%d\n", strlen(arr[1]));
// Report errors , The second element is passed , Same as the above value
printf("%d\n", strlen(&arr));
// Random value , It's the same as before , It's the whole address , The entire address starts with the address of the first element , Random values will be generated at random , Because it starts from the first element , So the result will be the same as the first
printf("%d\n", strlen(&arr + 1));
// Random value , Here is skipping the entire array , Then start calculating , encounter \0 Then stop , It is different from the first random value 6
printf("%d\n", strlen(&arr[0] + 1));
// Random value , Calculate from the address of the second element , encounter \0 stop it , It is different from the first random value 1
sizeof() And string arrays
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));
//7, Yes \0
printf("%d\n", sizeof(arr + 0));
//4 or 8, The address size is calculated , Start with the address of the first element
printf("%d\n", sizeof(*arr));
//1, What we calculate is the size of the first element
printf("%d\n", sizeof(arr[1]));
//1, It calculates the size of the second element
printf("%d\n", sizeof(&arr));
//4 or 8, The whole address is calculated
printf("%d\n", sizeof(&arr + 1));
//4 or 8, After skipping this array , Calculate the size of the following address
printf("%d\n", sizeof(&arr[0] + 1));
//4 or 8, After skipping the first element , Calculate the size of the following address , Instead of the size of the contents in the array
sizeof And string arrays
char arr[] = "abcdef"; printf("%d\n", sizeof(arr)); //7, Yes \0 printf("%d\n", sizeof(arr + 0)); //4 or 8, The address size is calculated , Start with the address of the first element printf("%d\n", sizeof(*arr)); //1, What we calculate is the size of the first element printf("%d\n", sizeof(arr[1])); //1, It calculates the size of the second element printf("%d\n", sizeof(&arr)); //4 or 8, The whole address is calculated printf("%d\n", sizeof(&arr + 1)); //4 or 8, After skipping this array , Calculate the size of the following address printf("%d\n", sizeof(&arr[0] + 1)); //4 or 8, After skipping the first element , Calculate the size of the following address , Instead of the size of the contents in the array
strlen And string
char arr[] = "abcdef";
printf("%d\n", strlen(arr));
//6, encounter \0 Stop printing
printf("%d\n", strlen(arr + 0));
//6, encounter \0 Stop printing
printf("%d\n", strlen(*arr));
// Report errors , It's not char * type
printf("%d\n", strlen(arr[1]));
// Report errors , Not wearing char* type
printf("%d\n", strlen(&arr));
//6, The address of the whole array is passed , Start with the address of the first element
printf("%d\n", strlen(&arr + 1));
// Random value , After spanning the entire array , Starting calculation encountered \0 stop it
printf("%d\n", strlen(&arr[0] + 1));
//5, Start with the second element , encounter \0 stop it
Character pointer and sizeof()
char* p = "abcdef";
printf("%d\n", sizeof(p));
//4 or 8, there p Is a pointer ,p Inside the village is the address of the first element ,
printf("%d\n", sizeof(p + 1));
//4 or 8, Point to the address of the second element
printf("%d\n", sizeof(*p));
//1, The size of the first element
printf("%d\n", sizeof(p[0]));
//1, The size of the first element
printf("%d\n", sizeof(&p));
//4 or 8,&p It's a secondary pointer
printf("%d\n", sizeof(&p + 1));
//4 or 8, Across the entire array
printf("%d\n", sizeof(&p[0] + 1));
//4 or 8, Calculate the size of the address from the second element
Character pointer and strlen()
char* p = "abcdef";
printf("%d\n", strlen(p));
//6,p It's the first element address
printf("%d\n", strlen(p + 1));
//5, Start with the second element
printf("%d\n", strlen(*p));
// Report errors , Because what you wear is a value , No char * Type of
printf("%d\n", strlen(p[0]));
// Report errors , It passes a value , No char * Type of
printf("%d\n", strlen(&p));
// Random value ,p Is a first level pointer ,&p It's just a secondary pointer , That is to say, here strlen It's from p The address of starts counting backwards \0 stop it
printf("%d\n", strlen(&p + 1));
// Random value , Across the entire array , Then start to calculate the encounter \0 Location
printf("%d\n", strlen(&p[0] + 1));
// Random value , Calculate from the first element encountered \0 until , The number that differs from the above is also a random value , Not a definite number , Because they met \0 The situation is different
// The value of the difference between the penultimate and the penultimate , It's a random value , because &p It's from p Starting number of addresses , When not &p[0]+1 when , There may be \0 It may not happen \0, and &p[0]+1, Start counting from the address of the second element , When did you meet \0 It's impossible
&p+1 and &p[0]+1 The reason why the difference of results is random , Are likely to p[0]+1 Move to the right and don't reach &p+1 When I met \0, It may also be with &p+1 Meet the same \0, Because of the \0 With uncertainty , So the difference between them is a random value .
Two dimensional arrays and sizeof()
int a[3][4] = { 0 };
printf("%d\n", sizeof(a));
//48, Here is the size of the entire array
printf("%d\n", sizeof(a[0][0]));
//4, Here we calculate the size of the first element
printf("%d\n", sizeof(a[0]));
//16,a[0] The size of the first line of elements
printf("%d\n", sizeof(a[0] + 1));
//4/8, there a[0] No longer put it alone () Inside , Yes +1 The operation of , So here a[0] Represents the address of the first element ,+1 It means the size of the address of the second element , So here is the address
printf("%d\n", sizeof(*(a[0] + 1)));
//4, This is the first line , The size of the second element
printf("%d\n", sizeof(a + 1));
//4/8,a Yes +1 The operation of , therefore a Represents the address of the first element , The first line ,a+1 It's the address on the second line , Because it's an address, it's 4/8
printf("%d\n", sizeof(*(a + 1)));
//16, The size of the element in the second line
printf("%d\n", sizeof(&a[0] + 1));
//4/8, The address on the second line
printf("%d\n", sizeof(*(&a[0] + 1)));
//16, The array size of the second row
printf("%d\n", sizeof(*a));
//16,a Represents the address of the first element ,*a Is to dereference , So it is 16
printf("%d\n", sizeof(a[3]));
summary
The meaning of array names :
1. sizeof( Array name ), The array name here represents the entire array , It calculates the size of the entire array .2. & Array name , The array name here represents the entire array , It takes out the address of the entire array .3. In addition, all array names represent the address of the first element .
边栏推荐
- Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
- Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
- ASP. Net to access directory files outside the project website
- lolcat
- Tkinter Huarong Road 4x4 tutorial II
- Explanation of for loop in golang
- pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
- Are there any principal guaranteed financial products in 2022?
- 什么是权限?什么是角色?什么是用户?
- Summary of small program performance optimization practice
猜你喜欢
ArrayBuffer
MATLAB小技巧(25)竞争神经网络与SOM神经网络
C语言指针面试题——第二弹
C语言指针经典面试题——第一弹
直方图均衡化
libmysqlclient.so.20: cannot open shared object file: No such file or directory
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
Leetcode (Sword finger offer) - 35 Replication of complex linked list
PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
How to batch change file extensions in win10
随机推荐
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
`Example of mask ` tool use
2022-2028 global tensile strain sensor industry research and trend analysis report
IIS configure FTP website
Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
Sort out the power node, Mr. Wang he's SSM integration steps
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
2022-2028 research and trend analysis report on the global edible essence industry
MATLAB小技巧(25)竞争神经网络与SOM神经网络
查看CSDN个人资源下载明细
《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
Get the source code in the mask with the help of shims
Golang Modules
品牌连锁店5G/4G无线组网方案
2022-2028 global probiotics industry research and trend analysis report
Four common methods of copying object attributes (summarize the highest efficiency)
Launpad | 基礎知識
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
《网络是怎么样连接的》读书笔记 - FTTH