当前位置:网站首页>[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

边栏推荐
- FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
- Strongly connected components of digraph
- [self management] time, energy and habit management
- MySQL foundation 06 DDL
- 如今少年已归来,人间烟火气最抚凡人心 复工了~
- leetcode 2097 — 合法重新排列数对
- 不登陆或者登录解决oracle数据库账号被锁定。
- Basic use of sringcloud & use of component Nacos
- MongoDB系列之MongoDB常用命令
- Leetcode 2097 - Legal rearrangement of pairs
猜你喜欢

Arduino DY-SV17F自动语音播报

MySQL --- 数据库查询 - 基本查询

Understanding and distinguishing of some noun concepts in adjustment / filtering

Basic use of sringcloud & use of component Nacos
![[Arduino experiment 17 L298N motor drive module]](/img/e2/4511eaa942e4a64c8ca2ee70162785.jpg)
[Arduino experiment 17 L298N motor drive module]

电话网络问题

leetcode 6103 — 从树中删除边的最小分数

Telephone network problems

FPGA - 7 Series FPGA internal structure clocking -04- multi area clock

【C语言】指针与数组笔试题详解
随机推荐
matlab 多普勒效应产生振动信号和处理
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
MySQL foundation 04 MySQL architecture
Specified interval inversion in the linked list
机器学习术语
465. 最优账单平衡 DFS 回溯
[C language] branch and loop statements (Part 1)
[Arduino experiment 17 L298N motor drive module]
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
kivy教程之在 Kivy App 中使用 matplotlib 的示例
Mongodb common commands of mongodb series
Button wizard play strange learning - go back to the city to buy medicine and add blood
Telephone network problems
(C语言)数据的存储
Kivy tutorial - example of using Matplotlib in Kivy app
用Go+绘制爱心给心爱的她表白
[untitled]
Inversion de l'intervalle spécifié dans la liste des liens