Interpreting pointers in C and language pointers

Source: Internet
Author: User

Interpreting pointers in C and language pointers

I think pointers are undoubtedly a challenge for many beginners who are new to C language learning. However, I think pointers are also an important feature of C language. Maybe you rarely see pointers in programming languages except C and C ++. C ++ also uses references rather than pointers. Pointers, as an efficient tool, are a double-edged sword-they can greatly improve the program efficiency, but they are the breeding ground of many bugs.

This may be why people disagree with pointers. Personally, I still like this feature because I often need to deal with hardware and some underlying software. At this time, the pointer shows its unique charm. There is a lot of pointer knowledge. There is a classic book called "C and pointer". If you are interested, you can read it. Here, I mainly summarize some methods to interpret pointers (to be honest, this is really confusing). On the one hand, I want to use it for my own queries, and on the other hand, I hope I can help others.

I. Basic Concepts

I will not introduce the basic concepts of pointers in detail, because many books have introduced them in great detail. Here I will only introduce a part. The Pointer Points to an address, and the pointer itself is an unsigned integer in most systems (4 bytes on 32 bit and 8 bytes on 64 bit ). The following uses an example to illustrate its mechanism:

In the above example, a pointer p is defined first, and its type is int. That is to say, it can only point to one int type variable, but not to other types of variables. Finally, we assign the address of variable a to p. In this process, two memory blocks are involved. One is to store the memory of the pointer p (the memory address can be obtained with & p ), one is the memory block that stores the value of a (the memory address can be obtained with & ). The value of p in the first memory is the value of & a after being assigned a value. Note that * (asterisk) can have any space or no space between the variable type and the variable name. For example, the following three methods are the same:

In the above example, a pointer p is defined first, and its type is int. That is to say, it can only point to one int type variable, but not to other types of variables. Finally, we assign the address of variable a to p. In this process, two memory blocks are involved. One is to store the memory of the pointer p (the memory address can be obtained with & p ), one is the memory block that stores the value of a (the memory address can be obtained with & ). The value of p in the first memory is the value of & a after being assigned a value. Note that * (asterisk) can have any space or no space between the variable type and the variable name. For example, the following three methods are the same:

Explanation:

Let's look at the following example:

2. array first address a, & a, & a [0]

Note: although the meanings of a, & a, & a [0] are different, their three values are equal!

Take int a [3] as an example:

Differences between the first address of the array element and the first address of the array: in fact, the first address of the array element and the first address of the array are the same, that is, & a [0] and a (and & a) are equal, but they have different meanings. After adding 1 to the first address of the first element, it is the first address of the second element (the reason why the first address is always said is because some types of storage occupy multiple addresses ), however, the first address of the array plus 1 is the address of the next array. The next array here is only used to indicate that the size of the entire array is added when 1 is added, rather than the size of an element.

It is easy to confuse: although a represents the entire array, (a + 1) represents the first address of the next element, that is, the same as (& a [0] + 1, the form of the next array is: (& a + 1 ). The following describes a program:

Output result:

Note (the following rows only count the rows with code in the main function ):

3. pointer arrays and array pointers

Pointer array: first, it is an array. The element of the array is a pointer and it also becomes an array for storing pointers ".

Array pointer: first, it is a pointer that points to an array. It can also be understood as an "array Pointer ". You can also use the previous "Interpretation Method" for analysis.

Iv. function pointers and pointer Functions

Function pointer: pointer to a function variable.

Pointer function: a function with a pointer, that is, a function that returns a pointer.

5. pointer constants and constant pointers

How to remember?

3 ~ Five omnipotent keys

In fact, there are 10 thousand key points for judging "pointer arrays and array pointers, function pointers and pointer functions, pointer constants and constant Pointers. That is, according to our powerful Chinese Syntax: the front side is the modifier, and the back side is the subject. For example, in the "pointer array", the pointer is only a modifier, And the array behind it is the subject, so it is an array.

Vi. Wild pointer

A wild pointer refers to a pointer with no definite direction. The following situations may cause wild pointers:

1. pointer variables are created but not initialized.

2. After the pointer p is free or deleted, It is not set to NULL.

Copyright Disclaimer: I would like to thank the original author for his hard work. Source: bole online column author-time track, link: http://blog.jobbole.com/102052 /.

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.