当前位置:网站首页>[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
边栏推荐
- R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
- excel IF公式判断两列是否相同
- MySQL foundation 04 MySQL architecture
- [自我管理]时间、精力与习惯管理
- 12_微信小程序之微信视频号滚动自动播放视频效果实现
- 2022 cable crane driver examination registration and cable crane driver certificate examination
- Key wizard play strange learning - multithreaded background coordinate recognition
- Specified interval inversion in the linked list
- leetcode:701. 二叉搜索树中的插入操作【bst的插入】
- Kivy tutorial how to create drop-down lists in Kivy
猜你喜欢
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
Machine learning terminology
excel表格计算时间日期的差值,并转化为分钟数
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
Telephone network problems
有向图的强连通分量
12_微信小程序之微信视频号滚动自动播放视频效果实现
【无标题】
Database SQL language 01 where condition
基本远程连接工具Xshell
随机推荐
Key wizard play strange learning - multithreaded background coordinate recognition
The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
Embrace the safety concept of platform delivery
First hand evaluation of Reza electronics rz/g2l development board
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
(C language) data storage
【无标题】
matlab 多普勒效应产生振动信号和处理
MySQL foundation 04 MySQL architecture
MySQL
关于Fibonacci数列
Understanding and distinguishing of some noun concepts in adjustment / filtering
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
MySQL
Usage of using clause in kingbases alter table
MySQL foundation 05 DML language
18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
[self management] time, energy and habit management
比较版本号
信息熵的基础