当前位置:网站首页>Pointer advanced --- pointer array, array pointer
Pointer advanced --- pointer array, array pointer
2022-07-06 08:20:00 【Pineapple cat Yena】
The pointer , Is the key to learn data structure well . Most students only master the basic concept of pointer , This week , Let's dig deeper knowledge of pointer . To be proficient in industry and to be idle in play , Not lazy , Don't waste , aggressive , Long term vision .
List of articles
Preface
- What is an integer pointer ?
- What is a secondary pointer ?
- What is an array pointer ?
- What is pointer array ?
- How are their usages different ?
In this section, , Let's solve these mysteries .
Tips : The following is the main body of this article , The following cases can be used for reference
1. The basic concept of pointer
The pointer is a Variable , What is stored is the... Of the indicated variable Address , Can also pass through Pointer dereference To find the corresponding address Content

The pointer stores Address , The memory occupied by the address is 4 Bytes (x64 Environmental Science ) or 8 Bytes (x86 Environmental Science ). The following code ,pa,pc,pf All occupy four bytes .

3. Since the size of the pointer is 4/8, So why int *pt,
char* pc And other types of pointers ?int * Is an integer pointer type ,
Indicates that the pointer points to int Data of type , char* Empathy
- Pointer type function 2: Different types of pointers , Add 1, Number of bytes skipped Is different .
int * Type pointer plus one , Skip four bytes ;
char * Type pointer plus one , Skip a byte ;
The same goes for other types , The number of bytes occupied by the data type is the same .
As shown in the figure below ,int * The pointer par+1, Skip four bytes , Point to the second of the array
Elements .char* Pointer to type pc1 Add one , Skip a byte , Pointing to an array
The second element .

2. Character pointer
The character pointer is Pointing character The pointer to . here , We understand through the topic .
- subject 1, For printing results
const char * p = "abcdef";
printf ("%s\n", p);
Let's think about it , What is the result of the above code ?
The result is that abcdef It's printed , Why? ?
here , The pointer p Deposit is The first element of the string is ’a’ The address of , When printing a string , You can start with the first address , meet ’\0’ end , Print string .
because "abcdef" Is a constant string , Add const modification , Prevent warnings .
2. subject 2. For printing results
const char* p1 = "abcdef";
const char* p2 = "abcdef";
char arr1[] = "abcdef";
char arr2[] = "abcdef";
if (p1 == p2)
printf("p1==p2\n");
else
printf("p1!=p2\n");
if (arr1 == arr2)
printf("arr1 == arr2\n");
else
printf("arr1 != arr2\n");
give the result as follows , Did you do it right ?
p1==p2
arr1 != arr2
Why? ?
because ,"abcdef" Is a constant string , Put it in the read-only area , Save only one copy . When p2 Also when pointing to a string , The program will not redefine a string ,p1 And p2 Pointing to The same area , therefore p1 == p2.
because arr1 And arr2 It is two independent spaces , although Same content , But the starting and ending addresses are different , So the two arrays are not equal .
3. Pointer array
Pointer array , It's essentially a Array , Every element of an array is Pointer types . The following code
int arr1[] = {
1,2,3,4,5 };
int arr2[] = {
2,3,4,5,6 };
int arr3[] = {
3,4,5,6,7 };
int* parr[3] = {
arr1, arr2, arr3 };
int * parr[3] ,parr Is an array , The array type is int * [3],
Open up three spaces , Every element is an integer pointer . Replace
First element address , Save it in the pointer array .
I want to print out the elements with this pointer array , What to do ?
We know , The array name is the first element address , Then the first address of each element has , It's not difficult to print it out .
In the pointer array parr in ,parr[1] That is to say a Array name ,
Namely arr1 The first address of the array , therefore arr1[i] It can be used
*(arr1+i) Express , It can also be used. *(parr[0]+i) Express ,
Empathy ,arr2[3] It can be used *(parr[1]+i) Express , The specific code is as follows
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
printf("%d ", *(parr[i] + j));
}
printf("\n");
}
4. The meaning of array names
In general , The array name represents the address of the first element , But there are two special cases .
- sizeof,sizeof( Array name ) Represents the memory size of the entire array .
int arr[10] = {0};
printf("%d", sizeof(a));
// The answer is 40, ten int Type element ,10*4=40
- & Array name , It takes out the address of the entire array
int arr[7] = {1,2,3,4,5,6,7};
int *p = &a+1;
printf("%d", *(p-1));
What is the output result of the above code ?
The answer is 7, Why? ?
&a+1, intend Skip the entire array ,p It's an array The back position 
The output is *(p-1), namely p Move an integer byte forward , Yes 7, Dereference pointer , Output 7.
5. Array pointer
Array pointer , It's essentially a pointer , Point to an array .
int arr[10] = { 0 };
int (*p2)[10] = &arr;
p2 Is a pointer , Point to int【10】 An array of types .
6. Array pointer application
Array pointers are mainly used for Two dimensional array . because The first element of a two-dimensional array is the element of the first row , It's a one-dimensional array , therefore , Reception time , When using the pointer , Array pointer is required to receive .
The following code , Pass the two-dimensional array to the function with the array name, that is, the first address , Because of two dimensions
The first address of the array is the first line element , It's a one-dimensional array , therefore , Required number
Group pointer receive .p It's an array pointer , The element type pointed to is int [5], One
A one-dimensional array with five elements .
p Save the address in the first line ,(p+i) That is the first. i+1 The address of the line ,
*(p+i) That is the first. i+1 The address of the element at the beginning of the line ,*(p+i)+j yes
a[i][j] The address of ,*(*(p+0)+j ) Namely a[i][j] The elements of .
int arr[3][5] = { 1,2,3,4,5,2,3,4,5,6,3,4,5,6,7 };
print2(arr, 3, 5);
void print2(int (*p)[5], int r, int c)
{
int i = 0;
for (i = 0; i < r; i++)
{
int j = 0;
for (j = 0; j < c; j++)
{
printf("%d ", *(*(p + i) + j));
//printf("%d ", p[i][j]);
}
printf("\n");
}
}
7. Array parameters
7.1 Two dimensional array parameters
int a[3][5];
fun11(a);// It is a one-dimensional array address
fun2(a);
void fun1(int a[][5])// The number of columns must be indicated
void fun2(int (*arr)[5])// Array pointer
Receive a two-dimensional array with a pointer ,int a[3][5]
The address of the first element of the array name , The first element of the two-dimensional array is the first line ,
Is the address of a one-dimensional array
void fun(int (*p)[5],int r,int c)
For printing printf("%d"*(*(p+i)+j)));
*(p+i) Equivalent to a row of array names ,p[i]
p, Pointer to array ,int (*)[5],p+1, skip 5 Elements
perhaps void fun(int a[][5],int r,int c)
Be careful , Rows of a two-dimensional array can be omitted , The number of columns cannot be omitted
7.2 One dimensional integer array passes parameters
int a[5]={
0};
fun1(a);
fun2(a);
void fun1(int a[])
{
}
void fun2(int *a)
{
}
7.3 One dimensional pointer array parameter transfer
One dimensional pointer array , Every element is The pointer , Pass the address of the first element of the pointer , use The secondary pointer receive . The secondary pointer , It stores the address of the first level pointer .
int *arr[5]={
0};
fun1(a);
void fun(int *arr[])
{
}
void fun(int **a)// The secondary pointer can store the address of the primary pointer
{
}
int *arr[5]={0};
fun(a);
void fun(int *arr[])
{}
void fun(int **a)// The secondary pointer can store the address of the primary pointer
{}
summary
There are many places where the knowledge points of pointer can be excavated , Need to be understood repeatedly . For a better future , Let's refuel ~
边栏推荐
- Introduction to number theory (greatest common divisor, prime sieve, inverse element)
- Nft智能合约发行,盲盒,公开发售技术实战--合约篇
- Easy to use tcp-udp_ Debug tool download and use
- Circular reference of ES6 module
- 从 SQL 文件迁移数据到 TiDB
- Analysis of pointer and array written test questions
- C语言自定义类型:结构体
- [research materials] 2022 China yuancosmos white paper - Download attached
- Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
- 21. Delete data
猜你喜欢

3. File operation 3-with

Database basic commands

指针进阶---指针数组,数组指针

National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
![[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached](/img/35/898a8086bc35462b0fcb9e6b58b86b.jpg)
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached

【MySQL】数据库的存储过程与存储函数通关教程(完整版)
![[research materials] 2022 China yuancosmos white paper - Download attached](/img/b4/422dff0510bbe67f3578202d6e80b7.jpg)
[research materials] 2022 China yuancosmos white paper - Download attached

Epoll and IO multiplexing of redis

Hcip day 16

21. Delete data
随机推荐
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
Hcip day 16
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
Nft智能合约发行,盲盒,公开发售技术实战--合约篇
[cloud native] teach you how to build ferry open source work order system
leetcode刷题 (5.28) 哈希表
MySQL view tablespace and create table statements
logback1.3. X configuration details and Practice
VMware virtualization cluster
Golang DNS write casually
A Closer Look at How Fine-tuning Changes BERT
leetcode刷题 (5.29) 哈希表
Online yaml to CSV tool
IOT -- interpreting the four tier architecture of the Internet of things
Yyds dry goods inventory three JS source code interpretation eventdispatcher
Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
LDAP應用篇(4)Jenkins接入
Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund
23. Update data
根据csv文件某一列字符串中某个数字排序