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

边栏推荐
- Custom annotation
- Can‘t connect to MySQL server on ‘localhost‘
- [transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
- 北京共有产权房出租新规实施的租赁案例
- What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry
- Visual upper system design and development (Halcon WinForm) -3 Image control
- redis缓存穿透,缓存击穿,缓存雪崩解决方案
- 【pytorch学习笔记】Datasets and Dataloaders
- Detailed comments on MapReduce instance code on the official website
- 基于SVN分支开发模式流程浅析
猜你喜欢

北京共有产权房出租新规实施的租赁案例

C语言刷题~Leetcode与牛客网简单题

Kubernetes will show you from beginning to end

Visual upper system design and development (Halcon WinForm) -3 Image control

Visual upper system design and development (Halcon WinForm) -5 camera

Summary of concurrent full knowledge points

Characteristics of MySQL InnoDB storage engine -- Analysis of row lock

基础SQL教程
![[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer](/img/9b/6ca8375ef8689a80d437665909ae30.png)
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer

Dataframe returns the whole row according to the value
随机推荐
Halcon and WinForm study section 2
XWiki Installation Tips
第04章_逻辑架构
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
北京共有产权房出租新规实施的租赁案例
[combinatorial mathematics] binomial theorem and combinatorial identity (binomial theorem | three combinatorial identities | recursive formula 1 | recursive formula 2 | recursive formula 3 Pascal / Ya
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
Leetcode sword offer find the number I (nine) in the sorted array
The first character of leetcode sword offer that only appears once (12)
[pytorch learning notes] datasets and dataloaders
Stress test WebService with JMeter
高并发下之redis锁优化实战
mysql innodb 存储引擎的特性—行锁剖析
XWiki安装使用技巧
Jvm-05-object, direct memory, string constant pool
[pytorch learning notes] transforms
Solve the problem that pushgateway data will be overwritten by multiple push
Construction of operation and maintenance system
Custom annotation
Reentrantlock usage and source code analysis