当前位置:网站首页>[advanced pointer I] character array & array pointer & pointer array
[advanced pointer I] character array & array pointer & pointer array
2022-06-12 08:37:00 【Always brush questions every day】

Catalog
1. Review of the contents of the primary pointer
2-1 What does the character pointer look like ?
2-3 The similarities and differences between code one and code two :
2-4 About character constant area :
3-1 What does the pointer array look like ?
3-2 Primary use ( Or let me show you the basic usage ):
4-1 Distinguish between address array name and array name ( It's a cliche )
4-2 Differentiate between array pointers and pointer arrays
4-3 Learned to ? Let's look at a small test question
5 Summary of test questions and rules
test 2: So the pointer array pointer ?
1. Review of the contents of the primary pointer
1. Memory is divided into small memory units , Each memory unit has a number , This memory number is called the address , Also called a pointer ( Memory number = Address = The pointer ).
2. A pointer variable is a variable , It's the address , Address can uniquely identify a memory unit .
3. The pointer variable size is fixed 4/8 Bytes (32/64 A platform ).
4. The pointer variable type determines (1) Pointer in +- How many bytes to skip when integer ;(2) The access permission of the pointer when dereferencing .
2. Character pointer
2-1 What does the character pointer look like ?
int main()
{
// Code 1 :
char ch = 'a';
char* p1 = &ch;
printf(" character 'a' The address of :>%p\n", p1);
printf(" Dereference the pointer to get the target pointed to by the pointer :>%c\n", *p1);
printf("\n\n\n");
// Code 2 :
char* p2 = "hello world";
printf(" character 'h' The address of :>%p\n", p2);
printf(" Dereference the pointer to get the target pointed to by the pointer :>%c\n", *p2);
printf("%s\n", p2);
return 0;
}
2-2 myth :
Mistake code 2 in p2 Pointer variables store strings , actually p2 It points to a string "abcdef” First character in 'a' The address of ( For two reasons :1. Pointer variable in x86 position (32 Under the machine ) yes 4 Bytes , however "abcdef“ Yes 7 Characters , A pointer can hold only one address ;2. Print out by pointer dereference 'a'), And because of the string "abcdef" In the memory ( Character constant area ) The space in is continuous , So just get the first element of the string 'a' Can access the contents of the entire string .
2-3 The similarities and differences between code one and code two :
1. Same as :
(1) Pointer types :p1 and p2 Are character pointer variables , All are stored in characters a The address of .
(2) Print character 'a: Print out 'a' All are dereferenced by getting the address stored in the character pointer variable , All we get is the variable pointed to by the pointer variable .
2. different
(1) character 'a' Storage location : Code 1 Medium 'a' Store in the stack area , Code 2 Medium 'a' Stored in the character constant area ( The screenshot below can prove )

2-4 About character constant area :

For the explanation of the figure above 3:
- Since it is located in the character constant area "abcdef" It is not allowed to modify
So in p2 There are hidden dangers when pointing to this memory space , Once you try to modify by dereference, you will cause a runtime error of the program , Program paralysis ;
- Therefore use const modification ( That is to say const char* p2="abcdef") To prevent dereference of a pointer and attempt to modify it , Give compile time errors in time , The program doesn't compile at all .
2-5 One is to distinguish the stack area from the character constant area && Character array and character pointer :
int main()
{
const char* ptr1 = "abcdef";
const char* ptr2 = "abcdef";
char arr1[] = "abcdef";
char arr2[] = "abcdef";
if (ptr1 == ptr2)
{
printf("ptr1==ptr2\n");//bingo
}
else
{
printf("ptr1!=ptr2\n");//error
}
if (arr1 == arr2)
{
printf("arr1==arr2\n");//error
}
else
{
printf("arr1!=arr2\n");//bingo
}
return 0;
}
explain :

3. Pointer array
In fact, the following pointer array and array pointer are of the same type ( Similar integer ,double,float)
3-1 What does the pointer array look like ?
// integer array
int arr1[5]={1,2,3,4,5};// An array of integers
// A character array
char arr2[5]={'a','b','c,''d','\0'};// An array of characters
// Pointer array : Pointers are embellishments , Arrays are nouns , Lead
int* arr3[5]={&arr1[0],&arr1[1],&arr1[2] ,&arr1[3] ,&arr1[4]};// An array of pointers
// The meaning of pointer array : The food is tasteless , Abandon a pity
// In fact, the effect is great , The meaning of our pointer array is similar to that of an ordinary array ,
// Both are convenient for the same type of element ( The type element here is a pointer ) Unified treatment : Such as modification and printing
3-2 Primary use ( Or let me show you Basic use ):
int main()
{
// It is easy to use :
int a = 10;
int b = 20;
int c = 30;
// Without array pointers
int* p1 = &a;
int* p2 = &b;
int* p3 = &c;
// There are array pointers
int* arr[3] = { &a,&b,&c };
for (int i = 0; i < 3; i++)
{
printf("%d\n", *(arr[i]));
}
return 0;
}

3-3 This is the correct way to use a pointer array !【 Pointer array simulation print two-dimensional array 】
This sum arr[3][5] The difference is that arr[3][5] The addresses of each element in memory are contiguous , And the address of the two-dimensional array simulated by the pointer array is not continuous .
int main()
{
int arr1[5] = { 1,2,3,4,5 };
int arr2[5] = { 2,3,4,5,6 };
int arr3[5] = { 3,4,5,6,7 };
int* arr[3] = { arr1,arr2,arr3 };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
//printf("%d\t",arr[i][j]);//way1
//printf("%d\t", *(arr[i] + j));//way2
printf("%d\t",*(*(arr+i)+j);//way3
// actually way1 and way2 It will be automatically converted to during the compiler translation way3
}
printf("\n");
}
return 0;
}


4. Array pointer
int main()
{
// Integer pointer - Pointer to an integer - The address where the integer variable is stored
int a = 10;
int* pa = &a;
// Integer pointer - Pointer to character - The address where the character variable is stored
char ch = 'w';
char* pc = &ch;
// Array pointer - Point to the address of the array , The address where the array variable is stored
int arr[5] = { 1,2,3,4,5 };
int(*p)[5]=&arr;// Pay attention to this 5 Don't omit , Because of this 5 Is the type of the array pointed to by the pointer
} 
A pointer can hold only one address , Only this address is the address of the whole array
4-1 Distinguish between address array name and array name ( It's a cliche )

4-1-1 sizeof( Array name ): It is the size of the memory occupied by the variable defined by the type , Unit is byte .
int main()
{
int arr1[5] = { 1,2,3,4,5 };
printf("%d\n", sizeof(arr1));//32 Under the platform :20 byte 64 Under the platform :20 byte // type ;int[5] Array
printf("%d\n", sizeof(arr1+0));//32 Under the platform :4 byte 64 Under the platform :8 byte // It is not alone sizeof( Array name )// type :int* The pointer
printf("%d\n", sizeof(arr1[0]));//32 Under the platform :4 byte 64 Under the platform :8 byte // type :int* The pointer
return 0;
}4-1-2 & Array name , In the absence of +- Integer, although the address is the same , But the meaning is different .

4-2 Differentiate between array pointers and pointer arrays
I will omit the picture !
int main()
{
// We know : Remove the variable name and the rest is the type of the variable
//( Here we can verify the following conjecture through debugging )
// integer array , The array type is int [5], Read from left to right is an integer array
// Particular attention : The number of elements in the array index 5 It's also part of the type
// therefore int [4] and int [5] Belong to different types
int arr[5] = { 1,2,3,4,5 };
//p1 First with the square 5( That is to say [5]) combination , So on the whole p1 Is an array
// Since it is an array, what we care about is the array. What does it store ?
// There are five elements in the array , The type of each element is int* type
// So the variable p1 The type of int* [5], Read from left to right is an array of pointers
int* p1[5] = { arr,&arr[1] ,&arr[2], &arr[3], &arr[4] };
//p2 The first and * combination , So on the whole p2 It's a pointer
// Since it's a pointer , What we care about is what the pointer points to ?
// The pointer points to an array , There are five elements in the array , For each element int type
// So the variable p2 The type of int[5]*, Read from left to right is array pointer
int(*p2)[5] = &arr;
return 0;
}
4-3 Learned to ? Let's look at a small test question


4-4 Take a look at a code that farts when you take off your pants 【 Take a look at the use of array pointers 】
void Print1(int arr[], int sz)
{
printf("Print1:\n");
for (int i = 0; i < sz; i++)
{
printf("%d\t", arr[i]);
}
}
void Print2(int* arr, int sz)
{
printf("Print2:\n");
for (int i = 0; i < sz; i++)
{
printf("%d\t", arr[i]);
}
}
void Print3(int(*p)[5],int sz)
{
printf("Print3:\n");
for (int i = 0; i < sz; i++)
{
printf("%d\t", *((*p)+i));
//printf("%d\t", (*p)[i]);
}
}
int main()
{
int arr[5] = { 1,2,3,4,5 };
int sz = sizeof(arr) / sizeof(arr[0]);
// Write a function to print the contents of the array
// test 1:
Print1(arr, sz);
printf("\n");
// test 2:
Print2(arr, sz);
printf("\n");
// test 3:
Print3(&arr,sz);
return 0;
}

4-5 This is the correct way to use array pointers 【 Array pointer simulates printing a two-dimensional array 】
Although the pointer array can int(*p)[3]=&arr; among arr It's a one-dimensional array , But that's too much ,
It's better to be direct int*p arr;
The real usage scenario of pointer array is left to the two-dimensional array when passing array names , Formal parameters are received with a pointer array .
void Print1(int arr[3][5], int row, int col)
{
printf("Print1:\n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
void Print2(int(*p)[5], int row, int col)
{
printf("Print2:\n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("%d\t", (* (p + i))[j]);
//printf("%d\t", *(*(p+i)+j));
//printf("%d\t",p[i][j]);
//p+i It's No i That's ok , That is the first. i Row the address of the entire array
//*(p+i) That is, dereference the address of the entire array , That is the first. i Row array name , That is the first. i The address of the element at the beginning of the line
//*(p+i)+j That is the first. i Xing di j The address of the column element
//*(*(p+i)+j) That is to say, for the first time i Xing di j Address dereference of column element , That is the first. i Xing di j The elements of the column
}
printf("\n");
}
}
int main()
{
int arr[3][5] = { {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7} };
// test 1:
Print1(arr, 3, 5);
printf("\n");
// test 2:
Print2(arr, 3, 5);
printf("\n");
return 0;
}
At present, the array pointer refers to a one-dimensional array
In fact, the array pointer can still point to a two-dimensional array :
int arr[3][5] = { {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7} };
int(*p)[3][5]=&arr;
The end , And the flower , wait , How about a little test ?
5 Summary of test questions and rules
test 1:
If int(*arr[3])[5];
problem : How to understand this code ?
answer : Array pointer array



test 2: So the pointer array pointer ?

A little rule I summarized
If light wants to name this thing above , Just focus on * and [] The order of appearance of
such as ; Array pointer array :[]*[]
Pointer array pointer *[]*
Then take it. [] The contents and * Just fill in the pointing thing
边栏推荐
- Engineers learn music theory (II) scale and tendency
- 深拷贝与浅拷贝的区别
- Vision transformer | arXiv 2205 - TRT vit vision transformer for tensorrt
- Vscade debug TS
- FDA审查人员称Moderna COVID疫苗对5岁以下儿童安全有效
- Install iptables services and open ports
- Installation series of ROS system (II): ROS rosdep init/update error reporting solution
- [compilation principle] understand BNF
- Hands on deep learning -- implementation of multi-layer perceptron from scratch and its concise implementation
- 记Date类型的一次踩坑
猜你喜欢

【 pointeur avancé Ⅲ】 mise en œuvre de la fonction de tri rapide qsort& fonction de rappel en langage C

企业上MES系统的驱动力来自哪里?选型又该注意哪些问题?

JVM学习笔记:垃圾回收机制

What is the quality traceability function of MES system pursuing?

Loading font component loading effect

svg中viewbox图解分析

报错:文件夹在另一个程序中打开无法删除怎么办

What exactly is APS? You will know after reading the article

What kind of sparks will be generated when the remote sensing satellite meets the Beidou navigation satellite?

(p17-p18) define the basic type and function pointer alias by using, and define the alias for the template by using and typedef
随机推荐
R loop assignment variable name
Triggers in MySQL
(p27-p32) callable object, callable object wrapper, callable object binder
Vscode 调试TS
[open source project] easycmd command graphical software
js实现页面加载后再刷新一次
Calling stored procedures in mysql, definition of variables,
Hands on deep learning -- Introduction to linear regression model
Shell basic syntax -- array
Seurat package addmodulescore is used for bulk RNA SEQ data
What is the quality traceability function of MES system pursuing?
In depth interpretation of 5g key technologies
(P13) use of final keyword
ctfshow web3
Where does the driving force of MES system come from? What problems should be paid attention to in model selection?
动态线段树leetcode.699
When converting tensor to ndarray in tensorflow, the run or Eval function is constantly called in the loop, and the code runs more and more slowly!
ctfshow web 1-2
(p17-p18) define the basic type and function pointer alias by using, and define the alias for the template by using and typedef
处理异常数据