当前位置:网站首页>Analysis of classical pointer and array written test questions in C language
Analysis of classical pointer and array written test questions in C language
2022-07-04 05:13:00 【real Wangyanbin】
Pointer and array written test question analysis
We have learned about arrays before , Pointer initial and advanced . After learning the above knowledge, I haven't practiced many topics , Let's take a look c Analysis of language classic pointer and array written test questions
One dimensional array
#include<stdio.h>
int main()
{
int a[] = {
1,2,3,4 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a + 0));
printf("%d\n", sizeof(*a));
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(a[1]));
printf("%d\n", sizeof(&a));
printf("%d\n", sizeof(*&a));
printf("%d\n", sizeof(&a + 1));
printf("%d\n", sizeof(&a[0]));
printf("%d\n", sizeof(&a[0] + 1));
return 0;
}
Let's review the basic knowledge before the topic exercise
sizeof() The calculation is the size of the space occupied by the data, and the unit is bytes .
The array name represents the address of the first element of the array , But there are two exceptions :
- sizeof( Array name ) It calculates the size of the entire array , Note that only the array name , Other operations are not the size of the entire array .
- & Array name , It takes out the address of the entire array ,+1,-1 Skip the entire array .
The size of the address is 4/8, Computer related .
title
#include<stdio.h>
int main()
{
int a[] = {
1,2,3,4 };
printf("%d\n", sizeof(a));//16
//sizeof( Array name ), It calculates the size of the entire array ,
// So the output value is 4*4=16
printf("%d\n", sizeof(a + 0));// 4/8
//sizeof() Inside a It doesn't exist alone , either &
// So the array name represents the address of the first element of the array
printf("%d\n", sizeof(*a));//4
//*a in a Is the address of the first element of the array ,*a What you find is the first element of the array
// The first element is shaping , Size is 4 byte
printf("%d\n", sizeof(a + 1));//4/8
//a Is the address of the first element of the array ,a+1 Is the address of the second element of the array .
// The size of the address is 4/8
printf("%d\n", sizeof(a[1]));//4
//a[1] The second element of the array , size 4 byte .
printf("%d\n", sizeof(&a));
//&a Is the address of the entire array , As long as the address size is 4/8
printf("%d\n", sizeof(*&a));//16
// There are two ways of understanding
//1.&a<-->int (*)[4]
// &a What you get is the address of the array name , The type is int (*)[4], It's an array pointer
// The array pointer points to an array ,*&a-->a
//
//2.& and * Offset ,*&a-->a
printf("%d\n", sizeof(&a + 1));// 4/8
//&a What we get is the address of the array ,&a<-->int (*)[4]
//&a+1 Is from an array a The address of skipped a backward (4 A plastic element ) Size of array .
//&a+1 Or the address , Yes, the address is 4/8 byte
printf("%d\n", sizeof(&a[0]));//4/8
//a[0] Array head element ,&a[0] Address of the first element of the array
printf("%d\n", sizeof(&a[0] + 1));// 4/8
//&a[0]+1, The address of the first element of the array +1--> The address of the second element of the array .
return 0;
}
A character array
Additional knowledge is needed ,strlen Is the function , The header file <string.h>, Is a special function to obtain the length of a string , The end flag of the string is ’\0’, When does the character array appear ‘\0’ Namely strlen Function to get the length of the string .
The first group –> A character array
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = {
'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr + 0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr + 1));
printf("%d\n", sizeof(&arr[0] + 1));
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr + 0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));
printf("%d\n", strlen(&arr[0] + 1));
return 0;
}
Code parsing
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = {
'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));//6
//sizeof( Array name ), The space occupied by the array ,1*6=6byte
printf("%d\n", sizeof(arr + 0));// 4/8
//arr+0, The array name does not appear separately in sizeof Inside , The array name represents the address of the first element of the array
//arr+0 The address of the first element of the array
printf("%d\n", sizeof(*arr));// 1
//*arr, Dereference the address of the first element of the array , Array head element .
// The first element of the array is of character type , The size is 1byte
printf("%d\n", sizeof(arr[1]));// 1
// The size of the first element of the array 1byte
printf("%d\n", sizeof(&arr));// 4/8
//&arr, Fetch the address of the entire array , It's an address
// Yes, the address size is 4/8
printf("%d\n", sizeof(&arr + 1));//4/8
//&arr + 1 Is the address after the array .
printf("%d\n", sizeof(&arr[0] + 1));//4/8
//&arr[0] + 1 Is the address of the second element of the array
printf("%d\n", strlen(arr));// Random value 1
// In the character array 'a','b','c','d','e','f'.
// After the character array '\0' I don't know where it is .
// So the size is random
printf("%d\n", strlen(arr + 0));/// Random value
//arr+0 Address of the first element of the array , and arr The effect is the same
//printf("%d\n", strlen(*arr));// Report errors
//*arr Is the first element of the array 'a',ASCII Code for 97
//strlen('a')-->strlen(97)
// The address is 97 The memory of belongs to the system , Users are not allowed to use .
// The address visited cannot predict the effect ,*arr It can be regarded as a wild pointer
// Changing the code will report an error
//printf("%d\n", strlen(arr[1]));// Report errors
//a[1]-->'b',
//strlen('b')-->strlen(98)
// The reason for the error is *a identical
printf("%d\n", strlen(&arr));// Random value 1
//&a For the address of the entire array , But address access still starts from the address of the first element
// So the result is also a random value
printf("%d\n", strlen(&arr + 1)); // Random value 1 - 6
//&arr+1, Skip the entire array , The address is the address after the array
// It is also a random value , But the random value is a random value 1-6
printf("%d\n", strlen(&arr[0] + 1)); // Random value 1 - 1
//&arr[0]+1 Is the address of the second element of the array , The result is also a random value
// But the random value is Random value 1-1
return 0;
}
character string
character string "abcdef", Stored in character array ,f Post tacit view ’\0’,
strlen Is to find the length of the string , Focus on... In the string \0, The calculation is \0 The number of characters that appear before
strlen It's a library function , For strings only
sizeof Only pay attention to the size of memory space , Don't care what's in the memory
sizeof It's the operator
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr + 0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr + 1));
printf("%d\n", sizeof(&arr[0] + 1));
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr + 0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));
printf("%d\n", strlen(&arr[0] + 1));
return 0;
}
title
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));//7, Array size
printf("%d\n", sizeof(arr + 0));//4/8 The address size of the first element of the array
printf("%d\n", sizeof(*arr));//1, The size of the first element of the array
printf("%d\n", sizeof(arr[1]));//1, The size of the second element of the array
printf("%d\n", sizeof(&arr));//4/8, The size of the entire array address
printf("%d\n", sizeof(&arr + 1));//4/8, The size of the address after the array
printf("%d\n", sizeof(&arr[0] + 1));//4/8, Size of the address of the second element of the array
printf("%d\n", strlen(arr));//6, The size of the string
printf("%d\n", strlen(arr + 0));//6
printf("%d\n", strlen(*arr));// error ,*arr=‘a’; Illegal access address
printf("%d\n", strlen(arr[1]));// error ,arr[1]='b'; Illegal access address
printf("%d\n", strlen(&arr));//6, The address of the entire array , What you get is also the address at the lowest address
printf("%d\n", strlen(&arr + 1));// Random value , Skipping the entire array, I don't know the address behind the array
//‘/0’ The location of the is uncertain
printf("%d\n", strlen(&arr[0] + 1));//5, The pointer jumps over 1 Bytes of character size
// Is the address of the second element , Calculate string length 6-1
return 0;
}
Constant string
What should be noted is that ,char* p = “abcdef”;p It's a pointer variable . p Stored "abcdef" Is the first address of the string , Instead of stored strings .p+1 What is skipped is the space occupied by a character
Exercises :
#include<stdio.h>
#include<string.h>
int main()
{
char* p = "abcdef";
printf("%d\n", sizeof(p));
printf("%d\n", sizeof(p + 1));
printf("%d\n", sizeof(*p));
printf("%d\n", sizeof(p[0]));
printf("%d\n", sizeof(&p));
printf("%d\n", sizeof(&p + 1));
printf("%d\n", sizeof(&p[0] + 1));
printf("%d\n", strlen(p));
printf("%d\n", strlen(p + 1));
printf("%d\n", strlen(*p));
printf("%d\n", strlen(p[0]));
printf("%d\n", strlen(&p));
printf("%d\n", strlen(&p + 1));
printf("%d\n", strlen(&p[0] + 1));
return 0;
}
title :
#include<stdio.h>
#include<string.h>
int main()
{
char* p = "abcdef";
printf("%d\n", sizeof(p));//4/8, The size of the pointer variable
printf("%d\n", sizeof(p + 1));//4/8, The address of the second character of the string constant
printf("%d\n", sizeof(*p));//1, The size of the first character of a string constant
printf("%d\n", sizeof(p[0]));//1, The size of the first character of a string constant
printf("%d\n", sizeof(&p));//4/8, Character pointer p The address of
printf("%d\n", sizeof(&p + 1));//4/8,+1 Skip the size of a character pointer ,
// But the space points to who is uncertain .
printf("%d\n", sizeof(&p[0] + 1));//4/8,p[0]-->*(p+0)-->*p
//&p[0]+1-->p+1, The address of the second character of the string constant
printf("%d\n", strlen(p));//6
printf("%d\n", strlen(p + 1));//5
printf("%d\n", strlen(*p));//err
printf("%d\n", strlen(p[0]));//err
printf("%d\n", strlen(&p));// Random value ,&p Is to take out the pointer variable p The address of ,
// Pointer to the variable p The address of the constant string is stored ,strlen The function takes the address of the string constant
// Think of it as a string , This is an uncertain value , The storage of the address of the constant string in the pointer variable
// Whether it is big end storage or small end storage is still uncertain , So the result is a random value
printf("%d\n", strlen(&p + 1));// Random value , But with the &p There is no relationship between two random values
//&p+1 Is to skip the size of a pointer , The address pointed to is the same as p It is not certain that '\0'.
// Or who didn't '\0', Still none '\0'.
printf("%d\n", strlen(&p[0] + 1));//5,&p[0]+1-->p+1
return 0;
}
Two dimensional array
Some knowledge that needs attention
A two-dimensional array can be regarded as an array of a one-dimensional array
The array name represents the address of the first element , The first element of a two-dimensional array is a one-dimensional array , So the array name of the two-dimensional array represents the address of the first row of one bit array
int a[3][4] = { 0 };
- a[0] It can be regarded as the address of the first row of a two-dimensional array , And so on a[1],a[2]
- arr Alone in sizeof() Inside , perhaps &arr Is the address of the entire two-dimensional array , The rest is arr[0] The address of , data type int(*)[4]
sizeof() Internal expressions do not operate , Just get the data type of the expression
Example :
#include<stdio.h>
#include<stdio.h>
int main()
{
int a[3][4] = {
0 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a[0][0]));
printf("%d\n", sizeof(a[0]));
printf("%d\n", sizeof(a[0] + 1));
printf("%d\n", sizeof(*(a[0] + 1)));
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(*(a + 1)));
printf("%d\n", sizeof(&a[0] + 1));
printf("%d\n", sizeof(*(&a[0] + 1)));
printf("%d\n", sizeof(*a));
printf("%d\n", sizeof(a[3]));
return 0;
}
analysis :
#include<stdio.h>
#include<stdio.h>
int main()
{
int a[3][4] = {
0 };
printf("%d\n", sizeof(a));//48
//3*4*4
printf("%d\n", sizeof(a[0][0]));//4
// The size of the first integer element of a two-dimensional array
printf("%d\n", sizeof(a[0]));//16
//a[0], Equivalent to the first row of one-dimensional array name ,sizeof(a[0])
// Is the size of the first row of the two-dimensional array
printf("%d\n", sizeof(a[0] + 1));//4/8
//a[0], Equivalent to the first row of one-dimensional array name
//a[0]+1, Equivalent to the address of the second element in the first row of a two-dimensional array
printf("%d\n", sizeof(*(a[0] + 1)));//4
//*(a[0] + 1)-->a[0][1]
// The size of the second element in the first row is calculated
printf("%d\n", sizeof(a + 1));//4/8
//a+1 The address of the element in the second row of the two-dimensional array
printf("%d\n", sizeof(*(a + 1)));//16
//*(a + 1)-->a[1], Equivalent to the second row of one-dimensional array name ,sizeof(a[1])
// Is the size of the second row of the two-dimensional array
printf("%d\n", sizeof(&a[0] + 1));//4/8
//&a[0]-->&*(a+0)-->a
//&a[0] + 1-->a+1
printf("%d\n", sizeof(*(&a[0] + 1)));//16
//&a[0] + 1-->a+1
//*(&a[0] + 1)-->*(a+1)-->a[1]
//a[1], Equivalent to the second row of one-dimensional array name ,sizeof(a[1])
// Is the size of the second row of the two-dimensional array
printf("%d\n", sizeof(*a));//16
//*a-->a[0] Is the address of the first row of the two-dimensional array
printf("%d\n", sizeof(a[3]));//16
//a[3] Is a two-dimensional array 4 The address of the line
// Although the two-dimensional array has no fourth row , But the code is not out of bounds
//sizeof() Internal expressions do not operate , Just get the data type of the expression
return 0;
}
边栏推荐
- 【MATLAB】MATLAB 仿真模拟调制系统 — FM 系统
- [matlab] matlab simulation - narrow band Gaussian white noise
- [matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
- Zkevm (12) state proof of appliedzkp
- 【MATLAB】通信信号调制通用函数 — 低通滤波器
- 【MATLAB】MATLAB 仿真 — 低通高斯白噪声
- With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?
- Simple g++ and GDB debugging
- ping端口神器psping
- LabVIEW错误对话框的出现
猜你喜欢
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
Automated testing selenium foundation -- webdriverapi
2022G2电站锅炉司炉特种作业证考试题库及答案
STM32F1与STM32CubeIDE编程实例-74HC595驱动4位7段数码管
Detailed comparison of Hynix emmc5.0 and 5.1 series
[QT] timer
PostgreSQL has officially surpassed mysql. Is this guy too strong!
Trie数-字典树
Yolov6 practice: teach you to use yolov6 for object detection (with data set)
Programming example of stm32f1 and stm32subeide -74hc595 drives 4-bit 7-segment nixie tube
随机推荐
企业级日志分析系统ELK(如果事与愿违那一定另有安排)
记几个智能手表相关芯片 蓝牙芯片 低功耗
flink1.13 sql基础语法(一)DDL、DML
【MATLAB】MATLAB 仿真模拟调制系统 — VSB 系统
2022年R2移动式压力容器充装复训题库及答案
Several smart watch related chips Bluetooth chip low power consumption
【MATLAB】MATLAB 仿真模拟调制系统 — AM 已调信号的功率谱与相干解调
Secondary vocational group network security - memory Forensics
[matlab] general function of communication signal modulation Fourier transform
[matlab] matlab simulates digital baseband transmission system - digital baseband transmission system
2022年A特种设备相关管理(电梯)考试题模拟考试平台操作
Unity is connected to the weather system
《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记
Detailed comparison of Hynix emmc5.0 and 5.1 series
如何构建属于自己的知识引擎?社群开放申请
Trie number dictionary tree
ping端口神器psping
2022 question bank and answers for safety management personnel of hazardous chemical business units
appliedzkp zkevm(11)中的EVM Proof
National vocational college skills competition (secondary vocational group) network security competition questions - Analysis