当前位置:网站首页>C primer plus learning notes - 5. Pointer
C primer plus learning notes - 5. Pointer
2022-07-24 05:01:00 【Charles Ren】
List of articles
What is a pointer
A pointer is a variable whose value is a memory address .
Analogy :int The value of a type variable is an integer . The value of a pointer type variable is an address .
int a = 4;// Variable a It's an integer
int * p = 0x3434523; // Variable p It's an address value
But the value of this address is generally unknown to us , Generally, it is handed over to the system after allocation , And then we use
int a = 4;
int *p = &a; // Because the system has given variables a Assigned an address , So we can get a The address of is then assigned to p
Here the pointer is p, We can also say pointer p Point to variable a.
* Operator
Operator * We also call it dereference operator .
Declare a pointer :
int *p; //p It's pointing int Pointer to type variable .
char *pc; //pc It's a point char Pointer to type
here , asterisk * Express Statement It means , The declared variable is a pointer . It's a statement p It's a pointer , This pointer points to int Type variable . In other words, stored in p The data type on the address is int.
pc The value pointed to is (*pc) yes char type .
int a = 4;
int *p = &a;
printf(%d, *p); //* Will find the address p Values that exist on
Pointer types
Pointer is actually a new type , He is not an integer type , It is the pointer type . such as p, Its type is pointer .int*p and char*pc in p and pc All are Pointer types , But the types of values stored on their addresses are different , One stored is int type , One stored is char type .
Not that the type of this pointer is char, Another pointer type is int. It should be said that they are all pointer types , The types of values pointed to are different .
int main(void)
{
int a = 1000;
int *p = &a;
printf("%p \n", p); // Print address 0x7ffeba874aac
printf("%d \n", *p); // 1000
char *pc = &a;
printf("%p \n", pc); // Print address 0x7ffeba874aac
printf("%d \n", *pc); // -24
printf("%p \n", p+1); //0x7ffeba874ab0
printf("%p \n", pc+1); //0x7ffeba874aad
return 0;
}
Embodiment is based on what : For example, in the above example, if int In units of , We print p The address of , And get p Place the value of the , With int Type fetch , Namely 4 Bytes to get 1000.
When we pc Pointer get a When the address of , Or take the value of this position , But at this time, we are char The type is taken in units , Then the value obtained is the value of one byte -24.
Let's look at the address +1, such as p+1 In the memory address, you will use int To move the smallest unit , Move 4 byte , That is, the address value will increase 4, and pc+1 Then only move 1 The byte address value only increases 1.
Personal understanding
The book does not say that pointer types are different , But I think it can be understood as different types of pointers , such as int Pointer to type , and char Pointer to type , These two pointers belong to different types , They move in their own units , And the values pointed to are of different types . There is nothing wrong .
The value of the address is forced
The address is represented by an unsigned integer . We can use uint_64t
I think an example can better understand pointer , Is to take a uint_64 The number of type is strongly converted to pointer type .
int main(void)
{
int a = 1000;
int *p = &a;
printf("%p \n", p); // Print address 0x7ffeba874aac
uint64_t val_p = (uint64_t)p; //p It's a pointer type , Let's give him a strong turn
printf("%lx \n", val_p); // 7ffeba874aac
// &a Is the value of an address , We can force it into a pointer type
printf("%lx \n", &a); // &a The value of is 7ffeba874aac
int *ip;
ip = (int *) &a; // such ip This is the address
printf("%p \n", ip); //0x7ffeba874aac
printf("%d \n", *ip); // 1000 We can get the address
int *ip2;
ip2 = (void *) &a; //z This address can be strongly converted to any pointer type
printf("%p \n", ip2); //0x7ffeba874aac
printf("%d \n", *ip2); // 1000 But when printing, we need to specify the type of the printed variable
printf("%zd \n", sizeof(int*)); //8
printf("%zd \n", sizeof(ip)); //8
printf("%zd \n", sizeof(void *));//8
return 0;
}
The size of pointer types is 8 byte .
边栏推荐
- mapreduce概念
- The opening ceremony of the 2022 Huawei developer competition in China kicked off!
- How to play the Microsoft twin tool twinsonot? Introduction to twin test tool twinornot
- 微信朋友圈的高性能架构设计
- PSO and mfpso
- Share a login interface template of QT implementation
- C. Recover an RBS (parenthesis sequence, thinking)
- Face algorithms
- Introduction to MapReduce
- 京东方高级副总裁姜幸群:AIoT技术赋能企业物联网转型
猜你喜欢

PSO and mfpso

最大公约数
![[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand](/img/9a/a218c46806cf286f0518a72809e084.png)
[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand

Kingbase V8R6集群安装部署案例---脚本在线一键缩容

Forward proxy, reverse proxy and XFF

Chapter III encog workbench

激活函数和最常用的10个激活函数

HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier

Kingbase V8R6集群安装部署案例---脚本在线一键扩容

MapReduce concept
随机推荐
Chapter 7 other neural network types
What is the sandbox technology in the data anti disclosure scheme?
The difference between statement and Preparedstatement and how to use placeholders
What if IPv4 has no internet access? Solutions to IPv4 without internet access rights (detailed explanation of pictures and texts)
How to get the signature file of Baidu Post Bar? Baidu Post Bar signature file setting and use method graphic introduction
Array force buckle (continuously updated)
Kingbase v8r6 cluster installation and deployment case - script online one click capacity reduction
微信朋友圈的高性能架构设计
To 3mm; Provide safe and stable product execution according to the sender IID
MapReduce介绍
想知道一个C程序是如何进行编译的吗?——带你认识程序的编译
[cornerstone of high concurrency] multithreading, daemon thread, thread safety, thread synchronization, mutual exclusion
frp内网穿透服务使用
How to make yourself look young in how old robot? How old do I look? Younger method skills
Sword finger offer special assault edition day 7
How much do you know about thread pool and its application
口叫SC 或者 pb 文件为读写控制ensor为
E d-piece system is nfdavi oriented, reaching a high level for engineers
How to download vscode using domestic image seconds
C language: selective sorting method