当前位置:网站首页>[C Advanced] pointer array vs array pointer
[C Advanced] pointer array vs array pointer
2022-07-27 13:43:00 【ecember】
Technical nerd , Save the world !
author :@ecember
special column :《 from 0 Start ——C Language 》
To readers : People who believe in miracles are as great as miracles
| Thank you for give the thumbs-up and Focus on , If you need to see my homepage column |
List of articles
1. Preface
The happiest thing in life is to fight , Not success , The most painful thing in life is laziness , Not failure . Hello everyone , Here is ecember. Today we are going to discuss the concept and application of pointer array and array pointer in pointer ( The following results are in VS2022 Chinese compiler ).
2. Character pointer
We know that there is a type of pointer called character pointer, that is char*
int main()
{
char ch = '0';
char* pc = &ch;
*pc = 'a';
printf("%c\n", ch);
return 0;
}
We have to use character pointer to store the address of characters .
Pen test 1
int main()
{
const char* p1 = "abcdef";// Constant string _ And const irrelevant
const char* p2 = "abcdef";// Will not open up another space
char arr1[] = "abcdef";
char arr2[] = "abcdef";// The space opened up by the array is two spaces
if (p1 == p2)
{
printf("p1 == p2\n");
}
else
{
printf("p1 != p2\n");
}
if (arr1 == arr2)
{
printf("arr1 == arr2\n");
}
else
{
printf("arr1 != arr2\n");
}
return 0;
}
What does the above code output ? Here we must know that when we store strings with pointers , It is actually the address of the first character of the stored string , here p1, p2 Point to the same string , Therefore, the contents stored are the same . And we specify Different arrays when , Even if Same content , Opened up in memory The address is different .
3. Pointer array
seeing the name of a thing one thinks of its function , An array of pointers is an array , An array of pointers .
int main()
{
// Shape array
//int arr[10];// Store integer
A character array
//char arr2[5];// Store characters
//int* arr[10];// An array of integer pointers
//char* ch[5];// An array of character pointers
int a = 10;
int b = 20;
int c = 30;
/*int* p1 = &a; int* p2 = &b; int* p3 = &c;*/
int* arr[3] = {
&a,&b,&c };
int i = 0;
for (i = 0; i < 3; i++)
{
printf("%d ", *arr[i]);// Dereference the contents of the array to get the data
}
return 0;
}

application
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* parr[3] = {
arr1, arr2, arr3 };
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
//printf("%d ", parr[i][j]);
printf("%d ", *(parr[i] + j));
}
printf("\n");
}
return 0;
}
there parr Array is pointer array , The address of the first element of the above three arrays is stored ,parr[ i ] It is equivalent to the array name of each array , I.e. on behalf of The address of the element at the beginning of each line , You can add j To access each element of the array .
4. Array pointer
Array pointers are pointers ? Or arrays ? The answer is : The pointer .
We are already familiar with :
Shaping the pointer : int * pint; A pointer that can point to shaped data . Floating point pointer : float * pf; A pointer that can point to floating-point data . The array pointer should be : can Pointer to array .
What about the basic structure of pointer array ?
int *p1[10];
int (*p2)[10];
//p1, p2 What are the differences ?
explain :p2 The first and * combination , explain p Is a pointer variable , Then point to a size of 10 An array of integers . therefore p It's a pointer , Point to an array , It's called Array pointer .
Pay attention here :[ ] Priority is higher than * The no. , So we have to add () To guarantee p The first and * combination .
For the priority of operators , You can see my blog : Operator details .
int main()
{
int arr[10] = {
0 };
int(*p)[10] = &arr;// Array pointer
//p That is, the array pointer type name ——int(*)[10] Adding one indicates skipping an array
return 0;
}
practice
int main()
{
char* arr[5] = {
0 };
char*(*p)[5] = &arr;
return 0;
}
among p What is it? ? First p The first and * Combine to form a pointer , Then we looked at another one behind [ ] Indicates that it points to a 5 Array of elements , What type of array elements ?——char * , So p Is an array element pointing to char* The pointer to the array of .
Little science popularization
&arr VS arr
What's the difference between the two ?
Usually , The array name we are talking about is the address of the first element of the array
But there are two exceptions :
1.sizeof( Array name ), This represents the entire array ,sizeof It calculates the size of the entire array
2.& Array name , Here is the address of the whole array
Application of array pointer
void print2(int(*p)[5], int c, int r)
{
int i = 0;
for (i = 0; i < c; i++)
{
int j = 0;
for (j = 0; j < r; j++)
{
//p + i Is pointing to the i Yes
//*(p + i) It's equivalent to getting the third i That's ok , Also equivalent to the second i Row array name
printf("%d ", *(*(p + i) + j));//p[i][j]
}
printf("\n");
}
}
int main()
{
int arr[3][5] = {
{
1,2,3,4,5},{
2,3,4,5,6},{
3,4,5,6,7} };
// Function print arr Array
print2(arr, 3, 5);// What passed here is equivalent to the line pointer of the first line , That's the address on the first line
return 0;
}
We know that the address of the first line is actually passed by the parameters of the two-bit array , that p + i It is equivalent to getting the No i The address of the line , And then I'll explain the quotation , It's equivalent to getting the array name , namely The address of the first element of each line , Re pass +j Then dereference is to access every element of the two-dimensional array .

5. Conclusion
Here we are , our 《 Pointer array VS Array pointer 》 It's near the end , I will continue to update later C Language related content , Endless learning , It will always be left to those who are prepared . I hope my blog can help you , If you like it, you can give the thumbs-up + Collection Oh , You are also welcome to point out my mistakes in the comment area at any time .
边栏推荐
- Application of responsibility chain model in transfer accurate valuation
- "Digital economy, science and technology for the good" talk about dry goods
- LeetCode报错及其解决方案
- 2、Citrix Virtual Apps and Desktops 2203剪贴板重定向策略
- [leetcode] 592. Fraction addition and subtraction
- leetcode——83,24; Machine learning - neural networks
- Fiddler bag capturing Tool + night God simulator
- How to maintain slip ring equipment
- Absolute positioning
- Echart line chart displays the last point and vertical dotted line by default
猜你喜欢

SNMP (Simple Network Management Protocol)

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

Design of network abnormal traffic analysis system

eBPF/Ftrace

Pat class B 1109 good at C (detailed)

Application of responsibility chain model in transfer accurate valuation

leetcode——83,24; Machine learning - neural networks

纵横靶场-图片的奥秘

常见分布式理论(CAP、BASE)和一致性协议(Gosssip、Raft)

libevent 之 evconnlistener_new_bind
随机推荐
echart折线图默认显示最后一个点以及纵向虚线
QT excellent open source project 13: qscintilla
Product manager experience 100 (XI) - Strategic Product Manager: model and methodology
Design of network abnormal traffic analysis system
Install the wireless network card driver
Keras深度学习实战——推荐系统数据编码
Cute image classification -- a general explanation of the article "what are the common flops in CNN model?"
使用碳刷的注意事项有哪些
clear
Construction and application of industrial knowledge atlas (3): commodity knowledge fusion
eBPF/Ftrace
SCI论文写作
浏览器内核模块组成
Absolute positioning
SCI thesis writing
Fiddler bag capturing Tool + night God simulator
opencv图像的缩放平移及旋转
"Digital economy, science and technology for the good" talk about dry goods
电滑环的常用类型
Come and watch, 17 practical skills of operation and maintenance~
