当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Detailed comparison of Hynix emmc5.0 and 5.1 series

Unity is connected to the weather system

c语言经典指针和数组笔试题解析

如何使用postman实现简单的接口关联【增删改查】

VSCode的有用插件

Flutter ‘/usr/lib/libswiftCore.dylib‘ (no such file)

TCP state transition diagram
![[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术](/img/87/e0469e280365ed0261e2b551ebd888.png)
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术

ETCD数据库源码分析——初始化总览

Capturing and sorting out external Fiddler -- Conversation bar and filter
随机推荐
中科磐云—数据分析与取证数据包flag
抓包整理外篇fiddler———— 会话栏与过滤器
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Notes on the paper "cross view transformers for real time map view semantic segmentation"
[matlab] matlab simulation modulation system SSB system
Automated testing selenium foundation -- webdriverapi
TCP state transition diagram
远程桌面客户端 RDP
【MATLAB】通信信号调制通用函数 — 傅里叶逆变换
Detailed comparison of Hynix emmc5.0 and 5.1 series
[matlab] matlab simulates digital bandpass transmission systems - QPSK and OQPSK systems
[matlab] matlab simulation - simulate the AM modulation process of the modulation system
appliedzkp zkevm(11)中的EVM Proof
PostgreSQL has officially surpassed mysql. Is this guy too strong!
加密和解密
电子元器件商城与数据手册下载网站汇总
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
2022广东省赛——编码信息获取 解析flag
Programming example of stm32f1 and stm32subeide -74hc595 drives 4-bit 7-segment nixie tube
中科磐云—模块A 基础设施设置与安全加固 评分标准