当前位置:网站首页>[C language] detailed explanation of pointer and array written test questions
[C language] detailed explanation of pointer and array written test questions
2022-07-03 01:19:00 【Ordinary person 1】
author :@ Ordinary person 1
special column :《C Language from 0 To 1》
In a word : In the past , All is prologue
explain : The past is irreparable , The future can change
List of articles
Preface
We have learned all the relevant knowledge points of pointer before , Start with the concept of pointer , Learn the meaning of pointer types , Operate the pointer , Then there is the pointer array , And the meaning of array names , And array pointers , A function pointer , Function pointer array, etc . We have learned the knowledge of pointer on the whole . In this blog, we will practice pointer and array topics , The whole process is full of content , Don't be distracted !

One dimensional array
#include <stdio.h>
int main()
{
// One dimensional array
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 analyze :
Before the start , We need to know sizeof() The size of bytes occupied by the calculated space , The array name is the first element address ( Two exceptions :1.& The array name represents the address of the entire array ,sizeof( Array name ) Represents the entire array )
#include <stdio.h>
int main()
{
// One dimensional array
int a[] = {
1,2,3,4 };
printf("%d\n", sizeof(a));//16
//sizeof( Array name ), The array name represents the entire array , It calculates the size of the entire array , Unit is byte
printf("%d\n", sizeof(a + 0));//4/8
//32 The platform is 4,64 The platform is 8
//a+0 Not a separate array name a, So at this time a Represents the address of the first element of the array , First element address +0 Or the first element address
// The address value is 4/8, The current platform is x86 platform , The answer for 4
printf("%d\n", sizeof(*a));//4
//*a Medium a Is the address of the first element of the array ,*a Is to dereference the address of the first element
// The size of the first element is 4 Bytes
printf("%d\n", sizeof(a + 1));//4/8
// there a Is the address of the first element of the array ,a+1 Is the address of the second element
// The size of the address is 4/8
printf("%d\n", sizeof(a[1]));//4
// The size of the second element is 4 Bytes
printf("%d\n", sizeof(&a));//4/8
//&a The address of the extracted array , Address of array , It's just an address
// The size of the address is 4/8
printf("%d\n", sizeof(*&a));//16
//&a What you get is the address of the entire array , That is to say ---int(*)[4], The dereference access to it is an array
// So the size is 16
// It can be simply understood as * and & Offset each other
printf("%d\n", sizeof(&a + 1));//4/8
//&a What you get is the address of the array
//&a+1 From an array a The address of skipped backwards (4 Of an integer element ) Size of array
//&a+1 Or the address , The address size is 4/8
printf("%d\n", sizeof(&a[0]));//4/8
// The size of the address of the first element
// The size of the address is 4/8
printf("%d\n", sizeof(&a[0] + 1));//4/8
//&a[0]+1 Is the address of the second element
// The address size is 4/8 Bytes
return 0;
}
A character array
1
#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 ), Size of array , by 6
printf("%d\n", sizeof(arr + 0));//4/8
//arr+0 Is the address of the first element of the array
printf("%d\n", sizeof(*arr));//1
// First element address dereference ,*arr Is the first element of the array , Size is 1 byte
printf("%d\n", sizeof(arr[1]));//1
// The size of the second element is 1 Bytes
printf("%d\n", sizeof(&arr));//4/8
//&arr Is the address of the array , Yes, the address is 4/8 Bytes
printf("%d\n", sizeof(&arr + 1));//4/8
//&arr+1 Is the address after the array
// The address is 4/8
printf("%d\n", sizeof(&arr[0] + 1));//4/8
//&arr[0]+1 Is the address of the second element
// The address size is 4/8
/**************************************************************/
printf("%d\n", strlen(arr));// Random value >=6
//'\0' The location is uncertain , So the output value is a random value
printf("%d\n", strlen(arr + 0));// Random value
//arr It's the first element address ,arr+0 Still the same , Still random
printf("%d\n", strlen(*arr));//errorr error
//strlen() The incoming address is , Pass in *arr when ,*arr It's the first element , Into ’a'
//97 It's not the address , So there's an error , Similar to the wild pointer problem
printf("%d\n", strlen(arr[1]));//error
// Pass in ‘b’ namely 98 The same result , There is a problem
printf("%d\n", strlen(&arr));// Random value
//&arr Get the address of the array ,'\0’ Not sure where , Still random
printf("%d\n", strlen(&arr + 1));// Random value
//&arr+1 Skip this array , Back '\0‘ The location of is still uncertain
// It can be understood as the random value above -6
printf("%d\n", strlen(&arr[0] + 1));// Random value
//&arr[0]+1 Represents the address of the second element , Back '\0' The location is still uncertain
return 0;
}
2
#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;
}
Code parsing :strlen() Find the string length , Be careful ’\0’ The number of characters that appear before
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "abcdef";
//a b c d e f \0
printf("%d\n", sizeof(arr));//7
// Array 7 Bytes
printf("%d\n", sizeof(arr + 0));//4/8
// here arr Represents the address of the first element of the array ,arr+0 unchanged
// The address size is 4/8
printf("%d\n", sizeof(*arr));//1
// The first element address is dereferenced as the first element , The size is 1 Bytes
printf("%d\n", sizeof(arr[1]));//1
// The byte size of the second element is 1
printf("%d\n", sizeof(&arr));//4/8
// The address size is 4/8
printf("%d\n", sizeof(&arr + 1));//4/8
// The address size is 4/8
printf("%d\n", sizeof(&arr[0] + 1));//4/8
// The address size is 4/8
/**********************************************************/
printf("%d\n", strlen(arr));//6
// The array length is 6
printf("%d\n", strlen(arr + 0));//6
// There is no change
//printf("%d\n", strlen(*arr));//error
// There is a problem
//printf("%d\n", strlen(arr[1]));//error
// There is a problem
printf("%d\n", strlen(&arr));//6
// Entire array , The length is 6
printf("%d\n", strlen(&arr + 1));// Random value
//&arr+1 After '\0‘ The location is uncertain
printf("%d\n", strlen(&arr[0] + 1));//5
// Start with the address of the second element
return 0;
}
3
#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;
}
Code parsing :
#include <stdio.h>
#include <string.h>
int main()
{
char* p = "abcdef";
printf("%d\n", sizeof(p));//4/8
//p Is a pointer variable , The size is 4/8
printf("%d\n", sizeof(p + 1));//4/8
// The address size is 4/8
printf("%d\n", sizeof(*p));//1
// It's the first element
printf("%d\n", sizeof(p[0]));//1
// First element
printf("%d\n", sizeof(&p));//4/8
// The address is 4/8
printf("%d\n", sizeof(&p + 1));//4/8
// The address is 4/8
printf("%d\n", sizeof(&p[0] + 1));//4/8
// The address is 4/8
/*********************************************************/
printf("%d\n", strlen(p));//6
// From the first element address to the end 6 Elements
printf("%d\n", strlen(p + 1));//5
printf("%d\n", strlen(*p));//error
printf("%d\n", strlen(p[0]));//error
printf("%d\n", strlen(&p));// Random value
// stay p From the angle of , hinder '\0' Not sure
printf("%d\n", strlen(&p + 1));// Random value
// Or random values
printf("%d\n", strlen(&p[0] + 1));//5
// Start with the address of the second element
return 0;
}
Two dimensional array
#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;
}
Code parsing :
#include <stdio.h>
int main()
{
int a[3][4] = {
0 };
printf("%d\n", sizeof(a));//48
//3 That's ok 4 Column ,12*4=48
printf("%d\n", sizeof(a[0][0]));//4
// First row, first column element
printf("%d\n", sizeof(a[0]));//16
// first line : That is, the size of one-dimensional array :4*4=16
printf("%d\n", sizeof(a[0] + 1));//4/8
//a[0] It represents the address of the first element of the entire one-dimensional array in the first row , That is, the address in the first row and the first column
//a[0]+1 It's the address in the first row and the second column , Yes, the address is 4/8
printf("%d\n", sizeof(*(a[0] + 1)));//4
// It's the elements in the first row and the second column , Size is 4 Bytes
printf("%d\n", sizeof(a + 1));//4/8
//a Although the address of the two-dimensional array , But it is not placed alone sizeof Inside , I didn't take the address
//a Represents the address of the first element , The first element of a two-dimensional array is its first row ,a It's the address on the first line
//a+1 Just skip the first line , Indicates the address of the second line
// The address is 4/8 Bytes
printf("%d\n", sizeof(*(a + 1)));//16
// Equivalent to the size of the second line
printf("%d\n", sizeof(&a[0] + 1));//4/8
//&a[0] What you get is the address on the first line
//&a[0]+1 What you get is the address on the second line
// The address size is 4/8
printf("%d\n", sizeof(*(&a[0] + 1)));//16
// What you get is the size of the second line
printf("%d\n", sizeof(*a));//16
//a Represents the address of the first element , It's the address on the first line
//*a It is the dereference of the address in the first line , What you get is the first line
// The size of the first line is 16
printf("%d\n", sizeof(a[3]));//16
// Some people here may say that the cross-border visit , There will be problems.
// But it doesn't :
// We did not operate on it , Just visit
// For example :int a = 10;sizeof(int);sizeof(a);
// We calculated a When , Just need to know a Only a few bytes are known for the type of
// about a[3] It's the same thing , The type is certain , So for 16
return 0;
}
summary
We need to understand what array names mean
know sizeof() and strlen() The role of
Understand and grasp the pointer , What is involved
Practice with these pointers and arrays , We have a new understanding of pointer , Achieved the purpose of this blog . That's all for now

边栏推荐
- 按键精灵打怪学习-回城买药加血
- Linear programming of mathematical modeling (including Matlab code)
- 12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
- MongoDB系列之MongoDB常用命令
- ROS2之ESP32简单速度消息测试(极限频率)
- Explain the basic concepts and five attributes of RDD in detail
- Correctly distinguish the similarities and differences among API, rest API, restful API and web service
- 【FH-GFSK】FH-GFSK信号分析与盲解调研究
- RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
- [shutter] animation animation (shutter animation type | the core class of shutter animation)
猜你喜欢

合并K个已排序的链表
![[fh-gfsk] fh-gfsk signal analysis and blind demodulation research](/img/8a/8ca80f51a03341c982d52980c54b01.png)
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research

MySQL foundation 04 MySQL architecture

MySQL foundation 05 DML language

Excel calculates the difference between time and date and converts it into minutes

机器学习术语

excel IF公式判断两列是否相同

Excel removes the data after the decimal point and rounds the number

Explain the basic concepts and five attributes of RDD in detail

Basic concept and implementation of overcoming hash
随机推荐
按键精灵打怪学习-回城买药加血
Key wizard hit strange learning - automatic path finding back to hit strange points
异步、邮件、定时三大任务
【C语言】指针与数组笔试题详解
机器学习术语
链表中的节点每k个一组翻转
Key wizard play strange learning - front desk and Intranet send background verification code
1038 Recover the Smallest Number
Create your first Kivy program Hello word (tutorial includes source code)
Every k nodes in the linked list are flipped
18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
[day 29] given an integer, please find its factor number
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Machine learning terminology
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
Canvas drawing -- bingdd
MySQL basic usage 02
matlab 多普勒效应产生振动信号和处理