C Language-pointers

Source: Internet
Author: User

C Pointer Basics

In C language, pointers are undoubtedly the most troubling. Learn the C language pointers today and leave some notes for personal reference.

The first thing to understand is, what is a pointer?

Pointer: is the variable used to store the memory address.

Regardless of the type of pointer, the memory address is stored, and all types of pointer variables are of the same size.

Two symbols related to pointers: & accessor . Take the address of a variable. * Represents a pointer and can also take the value of the variable that corresponds to the pointer.

Let's take a look at the simplest use of pointers.

The code explains the comparison clearly, to see the results of the printing, the first two lines of printing results are the same, so you can see that the pointer is stored in the variable i address.

Look at the next two lines of the printed result, as you can see. The results of these two lines are 3, the second-to-last line is better understood, is the value of the variable I, that is, 3; the last of the printed result is the value of the variable that corresponds to the address of the 3,*PI that takes the pointer variable pi store.

That's a little bit around, anyway, remember, the pointer is the address, and *PI is the value that takes the value of the variable that corresponds to the pointer.

In this case, one thing to understand is that no matter whether the value of I or the value of pi is changed, it does not affect each other.

That is, change the value of I, the value of pi will not change, this good understanding, I just changed the value of this variable, and I this variable is still in memory, or in that block of memory, so long as this I in the memory of the location (address) unchanged, the value of pi will not change.

At the same time, change the value of pi, I also will not change the value, for example, now an int type of j variable, and then the address of the J variable to pi, then just change the PI value, pi is stored in the variable J address, and the address and value of the variable I will not change any.

This is also the flexibility of the pointer, which can point to any variable of the type that the pointer corresponds to. (That is, the PI can point to any variable of type int and cannot point to another type of variable)

Let's look at a simple example, swapping two numbers.

If you do not use pointers, it is very simple to do a middle variable directly, you can exchange the values of the two variables. And the use of pointers, it is very simple, divert.

It is clear that the above comments are not used without pointers, needless to say.

The following is the use of pointers, all said divert, since the pointer pi is stored in the address of the variable i, then, *PI represents the value of the variable i, then it is good to do, in the preceding code, with *PI replace I do not better, the same truth, with the *PJ to replace the J is good.

Finally look at the following two methods, these two methods are a bit more to understand, or you say I know what is a pointer. That's the RIP.

Suppose I do not exchange the values of the two variables I j in the main function now, but instead call a method to exchange their values, which method should I call now?

Very simple, two are the same, since the pass in the value of the int type, then directly take the value of the pointer variable corresponding to it, that is: Swap (*PI, *PJ);

The second one is called Swap (pi, PJ);

Yes, the method call is correct, and this is called. But the only way to make the value exchange is the following, which is simple:

First look at the above method that code, the method body x y is a local variable, this method changes only the value of the local variable. and the main function does not change. Therefore, this function cannot exchange its value. (This is also a value pass)

Then look at the following method, the following method is possible, the following method why can it? Because the address is passed. Why would you send the address?

Well, let's give an example. How about an example? Actually this involves the memory, then give a look to match a little example of it!

There are two houses, the address is Renmin Road 1th and 8th Renmin Lu, 1th House contains a basketball, 8th house inside a football.

The first method is just like copying the ball directly. To exchange copies, that is worth passing, and the second method is to pass the address of two houses, tell you to exchange the ball in two houses. Then you run to house number 1th, pick up the ball, run to house number 8th, swap the ball, and take the ball back to house number 1th. It's so simple. This is the reference pass.

Summary:

How do I let a child function change the value of the main function?

1: The parameter of the child function is the value of an address.

2: The child function modifies the value of the variable corresponding to the address of the formal parameter.

3: The main function passes the value of the variable that is being modified to the child function when it is used.

Common errors with pointers:

1: Pointer variables cannot be used until they are assigned.

No assignment, that is, garbage value, in the programming is called the Wild pointer;

2: Pointers of different types cannot be converted to each other.

3: When using pointers, do not access data that has been reclaimed by the system.

After the completion of the child function, all local variables of the child function are reclaimed by the system, and if the value can be obtained, it is a shadow value, it is not guaranteed that the value is correct;

Pointers also have a flexible use, in Java, the return value can be a basic type, can also be an object, can also be a container class (arrays, collections, etc.), but in C is not allowed to return an array. So, we can use pointers to return more than 2 parameters.

How to return it? Very simple, since the container class can be loaded with multiple variables, and C is no collection, that is, the array of Bai, the array will return one, how to return it? Return the address of the array directly not good!

The operation of the pointer:

Pointers can actually operate like other variables.

What's the joke, the pointer can also calculate, the pointer is not the address? What is the result of two address operations? For example, take the above example, Renmin Road No. 1th and Renmin Rd. 8th, what is the result of the operation? Is it 9th Renmin Road?

Yes, you are right, the pointer is of course not directly using the address to subtraction, the address of the subtraction is meaningless and does not allow this to do.

Since we cannot use pointers to subtraction, we add a * to each pointer, that is, to take the value of the pointer to do the operation, yes, that is true.

But this is the operation of the pointer, this is the value of the pointer to the operation, OK!

Since it is a pointer operation, that is the operation of the pointer, see the following example:

This is an example of a pointer operation, to see the last two lines of the printed statement, the variable i is not a pointer variable, pointer variables +1 and 1 is not the operation of the pointer?

What you can see is: the first line of the print, the value corresponding to the pointer I, that is, the array arr subscript 2 value (the third in the array, that is, 3).

It's a good idea to figure this out, pointers +1 and 1 represent values that correspond to the adjacent address of the pointer variable. How do you understand this sentence? That is, +1 means taking the next of the position, and 1 means taking the previous one, which is better understood.

But how is it made? How to identify it? An int is 4 bytes. How does it automatically shift up or down by 4 bytes?

Do not forget that the pointer is also a data type, although the pointer is stored in the address, but this address can only save the pointer corresponding to the address of the variable, so, the pointer at the time of the operation offset by the pointer itself corresponding to the length of the variable to offset.

Multi-level pointers

To be blunt, it is the pointer, which can have n layers, haha.

Suppose there is a code like this:

int i=5;
int* pi=&i;
int** ppi=π

The first line is very good understanding, the second line we also know what is meant, next look at the third line;

The third line is the pointer, that is, the address of the pointer is stored inside, this how to understand it, the second line is very good understanding, the pointer variable is stored in the INT type variable i address, the same reason, since the variable i is in memory, then the variable pi is in memory actually exist, so the variable pi Also has the address, here is storing the pointer pi address. Actually, it's like you're looking for someone, I'm going to find Zhang San, and I'm going to the classroom. Zhang San not, the classmate said Zhang San went to the dorm (first hand), then I ran will dorm, classmate said Zhang San in the canteen (two level hands). If the canteen is not found, it is a level three hands. Ha ha

This is clear, and then it's done. Or the above example:

Pi stored in the address of the variable i, then, *PI is to take the value of the variable I, and the PPI store is the PI address, then *ppi actually take the value of pi (the address of the variable i), then to go through the PPI to take the value of the variable i, then you need to use **ppi. This is a good understanding, the above understanding clearly below is good understanding.

finally say: pointers to functions

A pointer can be a pointer to a variable, a pointer to an indicator, or a pointer to a function. Similarly, a pointer to a function holds the address of a function.

Look at the code:

This is a simple example code for a pointer to a function, and the comments in the code are also fairly well written.

It is important to note that the function must be written before the main function, because the function must be loaded into memory before it can know what the address of the function is, otherwise it will error.

The pointer base is just written here:

2016-10-18 21:30

C Language-pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.