当前位置:网站首页>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 modulation system FM system
- IP时代来临,电竞酒店如何借好游戏的“东风”?
- 拓扑排序和关键路径的图形化显示
- Programming example of stm32f1 and stm32subeide -74hc595 drives 4-bit 7-segment nixie tube
- Share some of my telecommuting experience
- ping端口神器psping
- Exercise bubble sort
- [matlab] matlab simulates digital baseband transmission system - digital baseband transmission system
- [matlab] matlab simulation modulation system SSB system
- PostgreSQL has officially surpassed mysql. Is this guy too strong!
猜你喜欢

C basic (VII) document operation

Simulink与Arduino串口通信

中科磐云—2022广东木马信息获取解析

Electronic components mall and data manual download website summary

关于solidworks standard无法获得许可 8544问题的总结

Zhongke panyun-d module analysis and scoring standard

Trie number dictionary tree

June 2022 summary

How to build your own knowledge engine? Community open application

Zhongke Panyun - module a infrastructure setting and safety reinforcement scoring standard
随机推荐
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre
Trie number dictionary tree
Error response from daemon: You cannot remove a running container 8d6f0d2850250627cd6c2acb2497002fc3
2022年T电梯修理操作证考试题库及模拟考试
COMP1721 Creating Classes
When using flash to store parameters, the code area of flash is erased, which leads to the interrupt of entering hardware error
[matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
[matlab] matlab simulation modulation system - DSB system
[matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
Roles of rollup components
PostgreSQL has officially surpassed mysql. Is this guy too strong!
Sécurité du réseau dans les écoles professionnelles secondaires - preuve de mémoire
自动化测试selenium基础篇——webdriverAPI
模拟小根堆
Useful plug-ins for vscode
Unity2D--人物移动并转身
【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre
【MATLAB】通信信号调制通用函数 — 带通滤波器
[matlab] matlab simulation modulation system - VSB system
小程序毕业设计---美食、菜谱小程序
