当前位置:网站首页>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 simulation - low pass Gaussian white noise
- 【MATLAB】MATLAB 仿真 — 模拟调制系统 之 AM 调制过程
- When using flash to store parameters, the code area of flash is erased, which leads to the interrupt of entering hardware error
- 全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
- 2022 t elevator repair operation certificate examination question bank and simulation examination
- Sécurité du réseau dans les écoles professionnelles secondaires - preuve de mémoire
- [matlab] general function of communication signal modulation bandpass filter
- KMP match string
- Trie数-字典树
- C basic (VII) document operation
猜你喜欢

【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre

Flutter calls Gaode map app to realize location search, route planning and reverse geocoding

LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052

Zhongke Panyun - module a infrastructure setting and safety reinforcement scoring standard

KMP匹配字符串

海力士EMMC5.0及5.1系列对比详解

2022广东省赛——编码信息获取 解析flag

Zhongke panyun-d module analysis and scoring standard

Capturing and sorting out external Fiddler -- Conversation bar and filter

《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记
随机推荐
STM32F1与STM32CubeIDE编程实例-74HC595驱动4位7段数码管
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
Capturing and sorting out external Fiddler -- Conversation bar and filter
We believe that the development of consumer Internet will still be limited to the Internet industry itself
2022危险化学品经营单位安全管理人员上岗证题库及答案
PostgreSQL has officially surpassed mysql. Is this guy too strong!
电子元器件商城与数据手册下载网站汇总
[matlab] matlab simulation modulation system - DSB system
Simulink与Arduino串口通信
中科磐云—D模块解析以及评分标准
Test cs4344 stereo DA converter
数据标注是一块肥肉,盯上这块肉的不止中国丨曼孚科技
【MATLAB】MATLAB 仿真数字基带传输系统 — 数字基带传输系统
[matlab] communication signal modulation general function - low pass filter
由于使用flash存放参数时,擦除掉了flash的代码区导致进入硬件错误中断
The first introduction, stages and methods of defense system breakthrough from the perspective of the red team
【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(余弦滚降成形脉冲)的眼图
NTFS security permissions
A summary of the 8544 problem that SolidWorks Standard cannot obtain a license
laravel 中获取刚刚插入的记录的id
