当前位置:网站首页>[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
边栏推荐
- The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
- 18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
- 看疫情之下服装企业如何顺势而为
- [self management] time, energy and habit management
- 软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
- Telephone network problems
- 不登陆或者登录解决oracle数据库账号被锁定。
- excel去除小数点后面的数据,将数字取整
- 2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
- Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
猜你喜欢
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
How to convert Quanzhi a40i/t3 to can through SPI
[untitled]
Basic remote connection tool xshell
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
leetcode 6103 — 从树中删除边的最小分数
Assets, vulnerabilities, threats and events of the four elements of safe operation
matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
安全运营四要素之资产、脆弱性、威胁和事件
MySQL foundation 04 MySQL architecture
随机推荐
leetcode 6103 — 从树中删除边的最小分数
Embrace the safety concept of platform delivery
鏈錶內指定區間反轉
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
Infrared thermography temperature detection system based on arm rk3568
异步、邮件、定时三大任务
2022.2.14 resumption
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
比较版本号
正确甄别API、REST API、RESTful API和Web Service之间的异同
excel去除小数点后面的数据,将数字取整
链表中的节点每k个一组翻转
看疫情之下服装企业如何顺势而为
Usage of using clause in kingbases alter table
Button wizard play strange learning - go back to the city to buy medicine and add blood
按键精灵打怪学习-前台和内网发送后台验证码
【FH-GFSK】FH-GFSK信号分析与盲解调研究
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
MySQL foundation 06 DDL
[day 29] given an integer, please find its factor number