Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Pre-school taking

The animated version of this listening to the book on the example is really good! www.bilibili.com/video/BV1MJ…

preface

When I first entered the undergraduate school, I opened a data structure. After studying at B station, I found that it was difficult to understand the Pointers of C language without understanding them, so I reviewed the classic C Primer Plus

This book is really meticulous, plus reading and watching quality B station video, I have a little simple understanding of the pointer

The paper come zhongjue shallow, and must know this to practice

All right, here we go. Ollie gives us

Resources in this

It’s coming, it’s coming. The soul of C!

Yes, it’s a pointer

Dry goods!!

What is a pointer

Basically, a pointer is a variable whose value is a memory address, such as an int, which is an integer

Values of type char are characters

The value of a pointer variable is the memory address

Second, pointer usage

Use Pointers as function arguments, and why

Given that a pointer variable name is PTR, we could write the following statement:

ptr = &pooh; // Assign Pooh’s address to PTR

For this statement, we say that PTR “points to” Pooh.

The difference between PTR and &pooh is that PTR is a variable and & Pooh is a constant. Alternatively, PTR is a modifiable lvalue and & Pooh is an rvalue.

Of course, you can point the PTR elsewhere: PTR = &bah; Now the value of PTR is the bah address. To create a pointer variable, declare the type of the pointer variable. To declare PTR as a pointer to the address of a variable of type int, use the following new operator.

2.1 The indirect operator *

Given that PTR points to BAH,

As follows:

ptr = &bah; The indirect operator * (indirection operator) is then used to find the values stored in BAH, which is sometimes called the dereferencing operator. The indirection operator is not to be confused with the binary multiplication operator (*); although they use the same symbol, they have different syntactic functions.

val = *ptr; // Find the value to which PTR points

Statement PTR = &bah; And val = * PTR; Taken together, it is equivalent to the following statement;

val = bah

My understanding:

Pointer variable is stored in the memory address, since it is stored, then there is the operation for the retrieval, how to retrieve? Just prefix the pointer variable with *!

Summary:

The address operator: &

When followed by a variable name, the & gives the address of the variable

Example:

&nurse indicates the address of the variable nurse

Address operator: *

When followed by a pointer name or address, * gives the value stored at the address to which the pointer points

Example:

nurse = 22;

ptr = &nurse; // Pointer to nurse

val = *ptr; // Assign the value at the address pointed to by PTR to val

The result of the above three statements is to assign 22 to val

2.2 Declaring Pointers

When declaring a pointer variable, you must specify the type of the variable to which the pointer points. Why does it sound so convoluted?

As I understand it:

The type of a pointer variable must be the same as the type of the variable to which the pointer points

Such as:

Int * PI; PI is a pointer to a variable of type int

Char * PC; // PC is a pointer to a variable of type char

float * pf, * pg; // pf, pg are Pointers to variables of type float

The type specifier indicates the type of the object to which the pointer points, and the asterisk (*) indicates that the declared variable is a pointer. int * pi; PI is a pointer, and * PI is an int

This diagram is viewed from the top down to make it easy to understand the process of using Pointers

* and the space between pointer names is optional. Typically, programmers use Spaces when declaring and omit them when dereferencing variables.

2.3 Use Pointers to communicate between functions

#include <stdio.h>

void interchange(int * u,int * v);// Declare an int pointer u and an int pointer v in the function



int main(void)

{

 // Solve the swap function problem

 int x = 5, y = 10;

 printf("Originally x = %d and y = %d.\n", x, y);

 interchange(&x,&y); // Send the address to the function

 printf("Now x = %d and y = %d.\n",x ,y);



 return 0;

}



void interchange(int * u, int * v)

{

 int temp;

 temp = *u;

 *u = *v;

 *v = temp;

}
Copy the code

Program analysis

Let’s start with function calls,

interchange(&x,&y); The two arguments here are not the values of x and y, but the addresses of x and y

This is followed by the exchange of addresses of variables via a temp variable

Summary:

Variables: name, address, and value

When you write a program, you can think of variables as having two properties: a name and a value (there are other properties, such as types, that are not discussed at this point).

After the computer compiles and loads the program, it assumes that a variable also has two properties: an address and a value. The address is the name of the variable inside the computer.

In short, ordinary variables take the value as the base and the address as the derived from the & operator, while pointer variables take the address as the base and the value as the derived from the * operator.