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

边栏推荐
- [combinatorics] combinatorial identities (recursive combinatorial identities | sum of variable terms | simple combinatorial identities and | sum of variable terms | staggered sums of combinatorial ide
- Popular understanding of linear regression (I)
- Custom annotation
- 【pytorch学习笔记】Datasets and Dataloaders
- What is one hot encoding? In pytoch, there are two ways to turn label into one hot coding
- Kubernetes - yaml file interpretation
- Jvm-04-runtime data area heap, method area
- Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
- What is machine reading comprehension? What are the applications? Finally someone made it clear
- 软件安装信息、系统服务在注册表中的位置
猜你喜欢

Reentrantlock usage and source code analysis
![[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018](/img/2b/b23aeab584f89be6678c0fe059d4b6.png)
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018

Kubernetes advanced training camp pod Foundation

从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍

【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器

Popular understanding of gradient descent

Solve the problem that pushgateway data will be overwritten by multiple push

Tensorflow realizes verification code recognition (I)

Jvm-05-object, direct memory, string constant pool
![[set theory] inclusion exclusion principle (complex example)](/img/9a/db5a75e27516378c31531773a8a221.jpg)
[set theory] inclusion exclusion principle (complex example)
随机推荐
Using TCL (tool command language) to manage Tornado (for VxWorks) can start the project
What is one hot encoding? In pytoch, there are two ways to turn label into one hot coding
[combinatorial mathematics] binomial theorem and combinatorial identity (binomial theorem | three combinatorial identities | recursive formula 1 | recursive formula 2 | recursive formula 3 Pascal / Ya
[combinatorics] combinatorial identities (recursive combinatorial identities | sum of variable terms | simple combinatorial identities and | sum of variable terms | staggered sums of combinatorial ide
XWiki安装使用技巧
Didi off the shelf! Data security is national security
socket.io搭建分布式Web推送服务器
Apache ant extension tutorial
PyTorch crop images differentiablly
Use of Tex editor
el-switch 赋值后状态不变化
Visual upper system design and development (Halcon WinForm) -3 Image control
Kubernetes - yaml file interpretation
秒殺系統3-商品列錶和商品詳情
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
Idea does not specify an output path for the module
秒杀系统2-Redis解决分布式Session问题
App全局异常捕获
Detailed comments on MapReduce instance code on the official website
视觉上位系统设计开发(halcon-winform)-6.节点与宫格