当前位置:网站首页>[advanced pointer (1)] | detailed explanation of character pointer, pointer array, array pointer
[advanced pointer (1)] | detailed explanation of character pointer, pointer array, array pointer
2022-07-03 05:38:00 【Sobtemesa】
All great actions and thoughts , There's a tiny beginning .
hello world.

Catalog
Array parameters 、 Pointer parameter
One dimensional array parameters :
Two dimensional array parameters :
The pointer passes the parameter
First level pointer parameter transfer
The secondary pointer transmits parameters :
Character pointer
Character pointers are pointer variables that point to character data . We know c There are integers in language , floating-point , Character type but no string type , We usually use character arrays to store strings .

in addition ,C The language also supports using a pointer to a string to represent a string .

Be careful , When assigning values like this ,“abcdef” by Constant string ,"abcdef" It stands for Constant , It's not about putting abcdef Put it in the pointer , Is actually the First character a Address assigned to p, So let's verify that

First , Let's start with Double quotes The role of :
1. Applied for space ( In the constant area ), Stored string
2. Add... At the end of the string '/0'
3. return Address
Why can string constants be assigned to character pointer variables ?
Because it defines an ordinary character pointer , There is no defined space to store "abc", So the compiler has to help us find a place to put "abc", obviously , Take this "abc" As a constant and put it in the constant area of the program is the most appropriate choice for the compiler ."abcdef" The value of is not the character itself , It's the return address , If it is an address, you can receive it with a pointer . So you can assign string constants to character pointer variables .
Be careful , When storing strings with character arrays ,"abcdef" Not a constant string , Defining a character array defines the space for storing strings , Character array is to store characters one by one , So the compiler
will char [] = "abcdef" It can be interpreted as char[]={'a',b'','c','d','e','f','\0'};
because “abcdef” Is a constant string , therefore Its value cannot be modified , as follows , Program crash .

We'd better use const Decorate it , The program will not make mistakes

Do an exercise ~
What is the output ?


analysis : Creating arrays requires space , Different arrays need to open up different spaces , therefore arr1 and arr2 The memory space is different , The array name is the first element address , Different space , Address different , therefore arr1 !=arr2; and “abcdef” Is a constant string , Stored in the constant area , Do not modify the , Fixed address , Is the first character a The address of , therefore p1==p2.
Pointer array
The pointer array is Array , Is an array of addresses .
[ ] The priority ratio * high , therefore parr With the first [ ] combination , Represents an array , Array elements are Pointers .

Analogical analysis :

The application of pointer array :

A profound ~

Array pointer
What is an array pointer ? The same thing , First , It's a pointer . It's an array .
The pointer to the array is the array pointer .

Analogical analysis :
int *p=NULL;//p It's a plastic pointer -- Pointer to the shape -- Can store plastic address
char* pc=NULL;//p It's a character pointer -- Pointer to character -- An address where characters can be stored
char (*p)[10];//p It's an array pointer -- Pointer to array -- The address where the array can be stored Application of array pointer

Here we need to pay attention to ,&arr It takes out the address of the entire array , namely p Pointer is arr The address of , namely p==&arr;&arr And array names arr Meaning is different .*p Dereference operation makes it get this array , namely *p==arr,arr For array name ( First element address )
This is too awkward and troublesome. We don't usually use it like this , Array pointer is efficient only when it is used above two-dimensional array .
Let's take a look
There are two forms of array parameters :
The first one is :

The second kind :

Let's analyze the second form of parameter transmission :
Two dimensional array array array name first element address ; But what is the address of the first element of a two-dimensional array ?
When we say that the array name is the address of the first element , We need to Think of a two-dimensional array as a one-dimensional array , The first line is the first element , The second element in the second line , The first line is another one-dimensional array , The array has five elements , Each element is int, The address of the first element of the array name of a two-dimensional array is the address of the first line , Is the address of a one-dimensional array , So when passing parameters, we can use array pointer to receive .
Let's analyze three prints :

Play a wave of questions ~

The last parsing :

Array parameters 、 Pointer parameter
When writing code, we need to put Array perhaps The pointer Pass to function , How to design the parameters of the function ?
Array parameters :
One dimensional array parameters :

Two dimensional array parameters :

The pointer passes the parameter
First level pointer parameter transfer

reflection :
When the parameter part of a function is a first-order pointer , What parameters can a function accept ?
void test1(int* p) //test1 What parameters can a function accept ?
{}
void test2(char* p) //test1 What parameters can a function accept ?
{}
answer :1. The pointer 2. Variable address

The secondary pointer transmits parameters :

reflection :
When the parameter part of a function is a secondary pointer , What parameters can a function accept ?
1. The address of the first level pointer
2. The secondary pointer
3. Pointer array

边栏推荐
- 2022.DAY592
- Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver
- 小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
- [set theory] relational closure (relational closure related theorem)
- Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
- Sophomore dilemma (resumption)
- Go practice -- design patterns in golang's singleton
- About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller
- Go practice -- use redis in golang (redis and go redis / redis)
- Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
猜你喜欢

Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires

ES7 easy mistakes in index creation

College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN

Communication - how to be a good listener?
![[Shangshui Shuo series together] day 10](/img/a3/e8b9df588bef67ead925813a75c8c0.png)
[Shangshui Shuo series together] day 10

Win10 install pytullet and test

Shanghai daoning, together with American /n software, will provide you with more powerful Internet enterprise communication and security component services

"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released

期末复习(Day5)

Go practice -- gorilla / websocket used by gorilla web Toolkit
随机推荐
穀歌 | 蛋白序列的深度嵌入和比對
Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
redis 遇到 NOAUTH Authentication required
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
Altaro virtual machine replication failed: "unsupported file type vmgs"
Altaro set grandfather parent child (GFS) archiving
Explanation of several points needing attention in final (tested by the author)
NG Textarea-auto-resize
Go practice -- use redis in golang (redis and go redis / redis)
期末复习(DAY7)
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
Deploy crawl detection network using tensorrt (I)
中职网络子网划分例题解析
获取并监控远程服务器日志
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
Introduction to redis and explanation of data types
Final review (Day7)
Azure file synchronization of altaro: the end of traditional file servers?
Kubernetes resource object introduction and common commands (V) - (configmap)
Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver