C-language leak check---hands

Source: Internet
Author: User

This section will analyze the pointers in detail.


First, the pointer

The pointer is actually a variable, but what makes this variable unique is that the value he saved is an address.

int a = 4; int* p = &a;

The memory of this piece of code


As can be seen, p is essentially a variable, but this variable is special, his value is the address of a variable.

At this point, we can access the value saved by the address stored by the P variable via the * number, which can be a bit of a detour. To put it simply, that is, when *p does this, the reading is actually the value of a, which means that *p ==> 4.


Second, the value call and the address call

If you need to change the arguments inside the function body, you need to do so by using a pointer.

Consider a problem that uses a function to complete the two-number exchange. This is basically the function that we will write at the beginning of the C language.

int swap (int* A, int* b) {int c = *a;    *a = *b; *b = C;}

The function does not change the value of the argument when it is called, only the value of the argument is used inside the function.


Three, constant pointer

We've discussed the Const keyword before, which combines the const keyword with the pointer. There are several important points to note:

const int* p;            P variable, the content of P pointing is immutable int cosnt* p;            Same as last time, no difference int* const p;      P is immutable, the content of P points variable const int* const p; P is immutable, and the content of P is not variable.

For the combination of the memory const and the pointer, there is a formula, the left number right finger.



C-language leak check---hands

Related Article

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.