当前位置:网站首页>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;
}
边栏推荐
- 中職組網絡安全—內存取證
- Flutter ‘/usr/lib/libswiftCore. dylib‘ (no such file)
- Network equipment emergency response Guide
- [matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
- 2022广东省赛——编码信息获取 解析flag
- Yyds dry goods inventory TCP & UDP
- EVM proof in appliedzkp zkevm (11)
- 【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(余弦滚降成形脉冲)的眼图
- 【QT】定时器
- Simulated small root pile
猜你喜欢
![[QT] timer](/img/df/5db6af851ef19f33fd7e7a7ed46586.png)
[QT] timer

拓扑排序和关键路径的图形化显示

【QT】定时器

中職組網絡安全—內存取證

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

Flutter 调用高德地图APP实现位置搜索、路线规划、逆地理编码

NTFS security permissions

2022 Guangdong provincial competition - code information acquisition and analysis flag

电子元器件商城与数据手册下载网站汇总

KMP match string
随机推荐
[matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
[matlab] general function of communication signal modulation bandpass filter
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
National vocational college skills competition (secondary vocational group) network security competition questions - Analysis
Zkevm (12) state proof of appliedzkp
Li Kou's 300th weekly match
[matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
Unity 接入天气系统
自动化测试selenium基础篇——webdriverAPI
关于solidworks standard无法获得许可 8544问题的总结
2022G2电站锅炉司炉特种作业证考试题库及答案
Yolov6 practice: teach you to use yolov6 for object detection (with data set)
Annex II: confidentiality agreement for offensive and defensive drills docx
c语言经典指针和数组笔试题解析
Unity is connected to the weather system
【MATLAB】MATLAB 仿真数字带通传输系统 — ASK、 PSK、 FSK 系统
Secondary vocational group network security - memory Forensics
【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(第 I 类部分响应波形)的眼图
如何构建属于自己的知识引擎?社群开放申请
With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?
