当前位置:网站首页>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 .
边栏推荐
- 软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
- Can‘t connect to MySQL server on ‘localhost‘
- Digital image processing -- popular Canny edge detection
- Visual upper system design and development (Halcon WinForm) -1 Process node design
- C语言刷题~Leetcode与牛客网简单题
- Nppexec get process return code
- 详解指针进阶2
- [combinatorics] permutation and combination (set permutation, step-by-step processing example)
- What is label encoding? How to distinguish and use one hot encoding and label encoding?
- 整形和浮点型是如何在内存中的存储
猜你喜欢
VS2017通过IP调试驱动(双机调试)
Summary of JVM knowledge points
[cloud native training camp] module VIII kubernetes life cycle management and service discovery
秒杀系统1-登录功能
Halcon与Winform学习第一节
Unity hierarchical bounding box AABB tree
Tensorflow realizes verification code recognition (III)
The markdown file obtains the pictures of the network and stores them locally and modifies the URL
第04章_逻辑架构
求字符串函数和长度不受限制的字符串函数的详解
随机推荐
【pytorch学习笔记】Datasets and Dataloaders
Basic SQL tutorial
The markdown file obtains the pictures of the network and stores them locally and modifies the URL
Chapter 04_ Logical architecture
Matlab r2011b neural network toolbox precautions
redis单线程问题强制梳理门外汉扫盲
Introduction to redis master-slave, sentinel and cluster mode
Reentrantlock usage and source code analysis
Puppet automatic operation and maintenance troubleshooting cases
redis缓存穿透,缓存击穿,缓存雪崩解决方案
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
Tensorflow realizes verification code recognition (III)
北京共有产权房出租新规实施的租赁案例
整形和浮点型是如何在内存中的存储
Use of Tex editor
PyTorch crop images differentiablly
Functional modules and application scenarios covered by the productization of user portraits
Tensorflow realizes verification code recognition (II)
Puppet自动化运维排错案例
Visual upper system design and development (Halcon WinForm) -1 Process node design