Introduction to the C language pointer usage tutorial

  • 2020-04-02 02:44:08
  • OfStack

This article for C language beginners in detail about the use of Pointers, and with an example to explain. Specific analysis is as follows:

For starters in C, what is a pointer? The point is on one finger. Refers to what? Address. What address? The address of memory.

That's what Pointers are.

I'll explain it in more detail here. Data storage is to be stored in memory, that is, in memory to circle a field, in this field to put what you want to put. Variables care about what's in the field, they don't care about where it's in memory; The pointer CARES where the field is in memory, it doesn't care how big the field is, what's in it.

How do you use Pointers? Here are the basics:


int a, b, c;
double f;

int *pt_a = &a, *pt_b = &b, *pt_c = &c;
double *pt_f = &f;

The above two lines declare three int variables a, b, c, and a double variable f. Here's how to use the pointer. When you declare a variable, you only need to add the symbol "*" before the variable to indicate that it is a pointer, while the "int" before the variable indicates that the pointer points to an int variable in memory.

Declarations of variables and Pointers can also be put together:


int a, b, c;
double f;

int other, *pt_a = &a, *pt_b = &b, *pt_c = &c;
double x = 0, *pt_f = &f;

Then there's the new operator, the &. Familiar? Yes, this is the ampersand in scanf. It is the address character, that is, the operator in front of the variable to get the address of the variable in memory. This explains why scanf USES an "&" after the double quote, which means that the data read in the previous format is directly filled into memory according to the address given later, so the parameters provided after the scanf double quote are not really the variable that you want to assign, but the address you want to save the data. So, for example, if a variable of type int a has a pointer to it, pt, then the following two sentences are equivalent:


scanf("%d", &a);
scanf("%d", pt);

Go back to the pointer, and then we're going to change the value of the pointer to the variable, so how do we do that? Here's an example:


int a;
int *pt = &a;
*pt = 123; //This statement is equivalent to a = 123;

Don't forget the *.

As for the "*" symbol, it can be understood as follows: when you need to use the pointer to the content, add "*", it is the same as a variable; Just need the address of the pointer, do not need to add "*", it represents the address of memory.

The pointer can also change the address it points to:


int a, b, c;
int *pt_a = &a, *pt_b = &b;
pt_b = &c;
pt_a = pt_b;

A lot of textbooks emphasize the problem of the wild pointer, what is the wild pointer? Er... A pointer that can be called "wild." What does that mean? For example, if a pointer pt is declared without it pointing to any variable, where does it point? The answer is: not sure. It can point to the entire memory. If you change what it points to, and it happens to point to a critical process in the system... Then the consequences are more serious. So make sure the pointer points to something before you use it. What if you want it to point to nothing? Simply point to NULL.

So that's the pointer. I think a lot of people are going to be more bothered by the fact that you added a pointer to the variable, right? What I'm saying is, this is just the basic usage. In C arrays, there are also some complex data structures that require Pointers. So it is necessary to master these basic things.

I hope this article is helpful for you to study C programming language.


Related articles: