当前位置:网站首页>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 .
边栏推荐
- 12306 the most wonderful verification code in history: normal users can easily identify the ticket grabbing software and are rejected
- Esp32 tutorial (I): vscode+platform and vscade+esp-idf
- Pony activation tool appears cannot open file k:\oemsf solution
- Post SQL era: edgedb 2.0 Release Notice
- E d-piece system is nfdavi oriented, reaching a high level for engineers
- What is the sandbox technology in the data anti disclosure scheme?
- 排序——QuickSort
- Yum to see which installation package provides a command
- Icml2022 | rock: causal reasoning principle on common sense causality
- PostgreSQL: run PostgreSQL + pgadmin 4 in docker
猜你喜欢

Kingbase v8r6 cluster installation and deployment case - script online one click capacity reduction

想知道一个C程序是如何进行编译的吗?——带你认识程序的编译

Quick reference manual for the strongest collation of common regular expressions (glory Collection Edition)

一文带你深入浅出C字符串函数和内存函数

Web3 product manager's Guide: how to face the encryption world

How to solve the engine prompt alias herodb and game engine startup exceptions?

Uniapp learning

Smart pointer, lvalue reference, lvalue reference, lambda expression

What programmer is still being grabbed by the company at the age of 35? Breaking the "middle-aged crisis" of programmers

Introduction and use of pycharm debugging function
随机推荐
Little black leetcode journey: 100 same trees
What if the computer can't take screenshots? The solution to the problem that the shortcut screen capture key of the computer cannot be used
Esp32:arduino tutorial summary
力。操处于业务低峰期。进口调用会帮您准备时,每个字
mapreduce概念
The second chapter is encog's data acquisition
The difference between statement and Preparedstatement and how to use placeholders
格式问题处理
[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand
Yum to see which installation package provides a command
C. Recover an RBS(括号序列,思维)
Transpose of array sparse matrix
Summary of common errors in wechat applet cloud development
PHP修改配置文件的两种方法
Add.Eslinctrc.js under SRC for the general format of the project
To 3mm; Provide safe and stable product execution according to the sender IID
Activation functions and the 10 most commonly used activation functions
What is the proper resolution of the computer monitor? Introduction to the best resolution of monitors of various sizes and the selection of different wallpapers
How to get the signature file of Baidu Post Bar? Baidu Post Bar signature file setting and use method graphic introduction
High performance architecture design of wechat circle of friends