当前位置:网站首页>Detailed pointer advanced 1
Detailed pointer advanced 1
2022-07-03 15:23:00 【Little snail rushes towards】
Catalog
3、 ... and & Array and array name
5、 ... and Array parameters 、 Pointer parameter
Preface
First know the pointer through the previous study , We know Pointer to the variable It's used to store the address , understand Secondary pointer variables It is used to store the address of the first level pointer variable , Wild pointer Cause of formation, etc , Next we will continue to learn more about pointer .
One Character pointer
Among the pointer types, we know that one pointer type is character pointer char*
#include<stdio.h>
int main()
{
char* p = "abcdefg";
return 0;
}For this code , I believe many people will ask , Isn't the pointer variable storing the address ? Yes? Character pointer You can store a string . Actually char*p What's in it is The first address of the string (a The address of the character ).

Now let's look at an interview question
#include <stdio.h>
int main()
{
char str1[] = "hello bit.";
char str2[] = "hello bit.";
const char* str3 = "hello bit.";
const char* str4 = "hello bit.";
if (str1 == str2)
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");
if (str3 == str4)
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");
return 0;
}
From this interview question, we can see , Different arrays store the same elements , The initial address of the element is different .
Character pointers store the same elements , The first address is the same , This kind of string is also called constant string .
Two pointer array
Pointer array So it's called Siyi The essence is array , Is an array of pointers .
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int arr1[] = { 1,2,3,4,5,6 };
int arr2[] = { 2,4,5,6,8,7 };
int arr3[] = { 3,4,5,6,8,9 };
int* ppr[3] = { arr1,arr2,arr3 };
return 0;
}Among them ppr Is the pointer array .
3、 ... and & Array and array name
arr and &arr What's the difference ? We know arr It's an array name , The array name indicates the address of the first element of the array . that &arr What is the array name

Visible array name and & The address printed by the array name is the same . Are the two the same ?
We are looking at the following code

According to the above code, we find that , Actually &arr and arr, Although the values are the same , But the meaning is different . actually : &arr Represents the address of the array , Instead of the address of the first element of the array .( Feel it carefully ) In this case &arr The type is : int(*)[10] , Is an array pointer type Address of array +1, Skip the size of the entire array , therefore &arr+1 be relative to &arr The difference is 40.
The array name is the first element, and the address is usually .
But there are two special cases :
1sizeof( Array name ) The array name here represents the entire array , The calculation is The size of the entire array .
2& Array name , The array name here still represents Entire array .
We mentioned array pointers above , You may not know much about , Next we will start with what is array pointer , How can array pointers be used to explain .
Four Array pointer
Array pointer Essence pointer , Array pointer Is able to Pointer to array .
Now we can see the following code
#include<stdio.h>
int main()
{
int* p1[10];
int(*p2)[10];
return 0;
}about p1 Come because [] Is greater than * therefore p1 The first and [] combination , therefore p1 Is an array .
about p2 Come on *p2 By () therefore * and p2 First combine so p2 Is a pointer .
We know that array pointers are pointers , It is a pointer to an array . So how do we use it anyway ?
We also know that the first element of a two-dimensional array is its first line , That is, the address in the first line , Then we can use array pointers to receive . The code is as follows :
#include<stdio.h>
void print(int(*arr)[5], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("%d ", arr[i][j]);// Print the elements of each line
}
printf("\n");
}
}
int main()
{
int arr[3][5] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
print(arr, 3, 5);
//arr It is the address of line
return 0;
}So the array pointer can be used to receive the address of the first row of the two-dimensional array .
5、 ... and Array parameters 、 Pointer parameter
When we write code, we will pass the parameters of array or pointer to the function , Then how to design the parameters of the function ?
We can see the following code :
One dimensional array parameters
#include <stdio.h>
void test(int arr[])// The way 1
{}
void test(int arr[10])// The way 2
{}
void test(int* arr)// The way 3
{}
void test2(int* arr[20])// The way 1
{}
void test2(int** arr)// The way 2
{}
int main()
{
int arr[10] = { 0 };
int* arr2[20] = { 0 };
test(arr);
test2(arr2);
}about test1() function Come on , We can easily understand the way 1 And way 2 Writing , But for the way 3, Because we know that arr Is the address of the first element , We use pointers to receive , This is also very easy to understand .
about test2() function Come on , It's an array of Pointers , The way 1 There is no problem receiving by array pointer , But for the way 2 It's also possible , Then how will we understand it ? The way 2 It is used. Secondary pointer mode To receive , The secondary pointer stores the address of the primary pointer ,test2 Function passed arr2 To come over ( Address of the first element of the array ), This array is a little special , Each of his elements is int*( Equivalent to a first level pointer ), So we use secondary pointer to receive parameters arr2 Yes. .
We know that the parameters of one-dimensional array can be used Receive in the form of an array , It can also be used. Receive in the form of a pointer , So what can be used to receive the parameters of a two-dimensional array ?
Two dimensional array parameters
void test(int arr[3][5])// The way 1
{}
void test(int arr[][])// The way 2
{}
void test(int arr[][5])// The way 3
{}
// summary : Two dimensional array parameters , Function parameter design can only omit the first [] The number of .
// Because for a two-dimensional array , I don't know how many lines there are , But you have to know how many elements in a row .
// So it's easy to calculate .
void test(int* arr)// The way 4
{}
void test(int* arr[5])// The way 5
{}
void test(int(*arr)[5])// The way 6
{}
void test(int** arr)// The way 7
{}
int main()
{
int arr[3][5] = { 0 };
test(arr);
}For the above two-dimensional array , Only way 1, The way 3, The way 6 Parameter design is feasible , Yes test Function of I passed. arr( The first 1 The address of the line ), The way 1 And way 3 All parameters are designed in the way of array ( As long as the group column must be written out ), For the way 6 Come on , Array pointer is the way to receive ( I've said that before ).
First level pointer parameter transfer
For the first level pointer to pass parameters , We pass a pointer to the function , Functions need to receive with pointers , Like the following code :
#include <stdio.h>
void print(int *p, int sz)
{
int i = 0;
for(i=0; i<sz; i++)
{
printf("%d\n", *(p+i));
}
}
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9};
int *p = arr;
int sz = sizeof(arr)/sizeof(arr[0]);
// First level pointer p, Pass to function
print(p, sz);
return 0;
}
So how can we use pointers to achieve the functions we want ? We can dereference the pointer to find the element we want , Point to another element by adding and subtracting pointers .
Now we can continue to think about a problem When the parameter part of a function is a first-order pointer , What parameters can a function take ?
void test1(int *p)
{} //test1 What parameters can a function take ?among test1 You can receive The pointer , Address of the first element of the array .
The secondary pointer transmits parameters
Is there any difference between the parameters of the secondary pointer ? In fact, when I use the first level pointer to transmit parameters, I need to use the first level pointer to receive , Then if I use the secondary pointer to transmit parameters, I need to use the secondary pointer to receive .
#include <stdio.h>
void test(int** ptr)
{
printf("num = %d\n", **ptr);
}
int main()
{
int n = 10;
int* p = &n;
int** pp = &p;
test(pp);
test(&p);
return 0;
}So when the parameter of the function is a secondary pointer , What parameters can be received ?
We can pass The secondary pointer , Be able to pass Pointer array .
summary
Here we know what a character pointer is , Pointer array , Array pointer , We also know how one-dimensional and two-dimensional arrays and pointers transfer parameters , In the next blog, I will continue to share the advanced knowledge of pointer .

边栏推荐
- Kubernetes - yaml file interpretation
- 运维体系的构建
- 求字符串函数和长度不受限制的字符串函数的详解
- Leetcode sword offer find the number I (nine) in the sorted array
- 视觉上位系统设计开发(halcon-winform)-5.相机
- [combinatorics] permutation and combination (set permutation, step-by-step processing example)
- Detailed pointer advanced 2
- Summary of concurrent full knowledge points
- [cloud native training camp] module VIII kubernetes life cycle management and service discovery
- 第04章_逻辑架构
猜你喜欢

Kubernetes vous emmène du début à la fin

Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
![MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)](/img/cd/2e4f5884d034ff704809f476bda288.png)
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)

Summary of JVM knowledge points
![[pytorch learning notes] datasets and dataloaders](/img/c0/9cd539caff34db3cccc44505bbe3c5.png)
[pytorch learning notes] datasets and dataloaders

【可能是全中文网最全】pushgateway入门笔记

秒杀系统2-Redis解决分布式Session问题

Popular understanding of gradient descent

What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding

基础SQL教程
随机推荐
Construction of operation and maintenance system
Using TCL (tool command language) to manage Tornado (for VxWorks) can start the project
leetcode_ Power of Four
Visual upper system design and development (Halcon WinForm) -6 Nodes and grids
Jvm-09 byte code introduction
Popular understanding of ovo and ovr
PyTorch crop images differentiablly
函数栈帧的创建和销毁
Tensorflow realizes verification code recognition (II)
The markdown file obtains the pictures of the network and stores them locally and modifies the URL
[set theory] inclusion exclusion principle (complex example)
Kubernetes - yaml file interpretation
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer
Redis single thread problem forced sorting layman literacy
Kubernetes带你从头到尾捋一遍
Leetcode sword offer find the number I (nine) in the sorted array
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
Nppexec get process return code
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
Calibre LVL