The world of C language (9)

foreword

Hello, C language (pointers)

Pointer is an important concept in C language, which greatly enriches the functions of C language.

Pointers are also the essence of the C language.

Before reading, at this stage, please think like this: pointer == address

content

foreword

1. What is a pointer?

2. Memory address (format control character "%p")


1. What is a pointer?

A pointer is an address.

But be aware that our perceptions may change as we learn.

2. Memory address (format control character "%p")

When data is processed in a computer, it must enter the computer's memory.

We usually refer to a byte-sized storage space in memory as a memory unit , and the number of memory units occupied by data of different data types is different.

A memory address represents the "location" of an object in memory,

As shown in the figure below, memory is composed of memory cells one after another.

These memory cells are numbered ( unique ), and the number is the memory address.

Through each address, we can find a "small house" one by one. 

The green box is a small room, and each small green box has a number.

Example:

int x=200;
printf("%d",x);

In a 32-bit machine, int occupies 4 bytes, so 4 memory units are allocated for x. (1-4)

When we call the printf() function to output, the function finds the address (number) of 1 in the memory, and then reads from 1 to 4, and then reads to 200. At this time, the value of 200 in x is output. 

This process can be called: accessing the value of the variable according to the variable address, this way of accessing the variable is called "direct access".

When it comes to direct access, there must be indirect access! 

(When creating space for variables, the address of the first byte (memory unit) is generally used as the address of the variable.)

Indirect access: operate through the address of a variable.

That is, to use pointers to access memory and manipulate addresses, that is, to find the variable that stores the address of the variable first, then take the address of the variable from it, and then take the value of the variable from the corresponding location.

So here we introduce

pointer variable

A variable that specifically stores the address of a variable is called a "pointer variable". Its value is the address of a variable.

for example:

int x=200;
int *p;
p=&x;
printf("%d",*p);

We define an integer variable x and assign a value of 200, and define a pointer variable (the * sign here means that this variable is a pointer variable)

p=&x, (& is the address character. The assignment expression here represents that the p pointer variable points to x, and p and x establish a pointing relationship)

The indirect operation variable x can be realized by using the pointing relationship.

That’s it for a brief introduction to pointers. Thank you for reading this. There are bound to be mistakes in the blog post. Please point it out actively.

Guess you like

Origin blog.csdn.net/m0_60653728/article/details/122136890