Interpreting pointers in C language

Source: Internet
Author: User

I think for many beginners of C language, the pointer is undoubtedly a difficult point. However, I think pointers are a particularly important feature of C language. Perhaps you rarely see pointers in programming languages other than C and C + +. In C + +, more references are used, not pointers. Pointers, as a highly efficient tool, can be described as a double-edged sword-well-used, may greatly improve the efficiency of the program, but the use of bad, is a lot of bug breeding ground.

This may also be the reason why people are mixed with pointers. Personally, I still like this feature because I need to deal with hardware and some of the underlying software often. At this time, the hands reflect its unique charm. Pointers are a lot of knowledge, there is a classic book called "C and the hands", if interested can read. Here, I mainly summarize some how to interpret the pointer (to tell the truth this thing is very easy to confuse) the method, on the one hand to do query, on the other hand, hope can give others some help.

First, the basic concept

As for the basic concept of pointers, I will not introduce them in detail, because there are many books that are introduced in detail. Here I only introduce a part. The pointer points to an address, and the pointer itself is an unsigned integer on most systems (4byte on a 32bit machine and 8byte on 64bit). The following is an example of how the mechanism works:

In the above example, a pointer p is defined, and its type is int, which means it can only point to a variable of type int, not to another type of variable. Finally, we assign the address of the A variable to p. In this process, two memory blocks are involved, one is the memory that holds the pointer P (with &P to get the memory address), and the memory block that holds the value of a (the memory address can be obtained with &a). The value of the first memory p is &a after the assignment statement. Another note is that * (asterisks) and variable types and variable names can have any space between them, or not. For example, the following three ways are the same:

In the above example, a pointer p is defined, and its type is int, which means it can only point to a variable of type int, not to another type of variable. Finally, we assign the address of the A variable to p. In this process, two memory blocks are involved, one is the memory that holds the pointer P (with &P to get the memory address), and the memory block that holds the value of a (the memory address can be obtained with &a). The value of the first memory p is &a after the assignment statement. Another note is that * (asterisks) and variable types and variable names can have any space between them, or not. For example, the following three ways are the same:

Interpretation method:

Take a look at the following example:

Second, the first address of the array a,&a,&a[0]

Note: The meaning of a,&a,&a[0] is different, but their three values are equal!

Take int a[3] As an example to illustrate:

    1. A as an rvalue, represents the first address of the first element of the array, not the array address. The address of a[0]. int i = (a+1), where a is the right value, so represents the first address of the first element, a+1 represents the first address of the next element, i.e. &a[1].

    2. A is the name of the entire array. So the value of sizeof (a) is sizeof (int) * 3 = 40, which represents the size of the entire array.

    3. &a is the first address of a, which is the first address of the entire array. So sizeof (&A) = 4. The &a+1 in int p = (int) (&A+1) represents the first address of the next array, which is clearly out of bounds.

    4. &a[0] represents the first address of the first element. So sizeof (&a[0]) = 4.

    5. &A[3], it is clear that the array is out of bounds, but how much is its sizeof? Also 4, because the keyword sizeof evaluation is at compile time, although there is no a[3] this element, but there is no real access to a[3], but based on the array element type to determine its value. So sizeof (A[3]) does not go wrong.

    6. A[-1] What does it mean? The first thing to understand is that the form of the subscript is parsed into a pointer form by the compiler, i.e. a[1] is parsed into (a+1). So, A[-1] is parsed into * (A-1).

The difference between the first address of the first element of the array and the first address of the array: In fact, the first address of the first element and the value of the first address are the same, that is, &a[0] and a (and &a) are equal, but the meaning is not the same. The first element of the first address plus 1, is the first address of the second element (the reason is always said that the first address, because some types of storage will occupy multiple addresses), but the first address of the array and 1 is "the next array Address", where the next array is only to illustrate the size of the whole array, not an element.

It is easy to confuse: a Although representing the entire array, but (a+1) represents the first address of the next element, that is, as with (&a[0]+1), the next array is in the form of: (&a+1). The following is a program to illustrate:

Output Result:

Description (The following line counts only the lines that have code within the main function):

    1. The 1th line of the program defines an integer array with 3 elements.

    2. Line 2nd Prints the size of the long type. Because I am 64bit, so a long is 8byte.

    3. Line 3rd Prints the values of * (a+1), and the values of the results and a[1] are equal. Note A although representing the entire array, but as an rvalue, does represent the first address of the first element.

    4. The 4th row output value is 12, which is the size of the entire array.

    5. The 5th line prints the size of an out-of-bounds element, without an error, verifying the 5th above.

    6. The 6th line prints a[-1] and * (A-1), and the output values are equal. Verify the 6th above.

    7. Line 7th prints A and &a[0], and the values are equal. Indicates that the first address of the array is equal to the first address of the first element.

    8. Line 8th prints A, (a+1), (&a+1), by the result can see the first element of the first address plus 1 is the size of an array element is added, and the first address of the array plus 1 is the size of an array.

Three, pointer arrays and arrays pointers

Array of pointers: first it is an array, the elements of the array are pointers, and they become "arrays of stored pointers".

Array pointer: First it is a pointer to an array, or it can be understood as "pointer to an array". You can also use the previous "interpretation method" to analyze.

Four, function pointers and pointer functions

function pointer: A pointer variable that points to a function.

Pointer functions: Functions with pointers, that is, functions that return pointers.

Five, pointer constants and constant pointers

How do you remember?

    1. You can remove the type name first, and then see who is near to the const and modify who.

    2. It is also possible to const on the left of the constant pointer, const on the right of the pointer constant.

Three to five of the universal key

In fact, the "pointer array and array pointers, function pointers and pointer functions, pointer constants and constant pointers," the judgment, there is a universal key. That is according to our strong Chinese grammar: The front is a modifier, behind the subject. For example, "pointer array", the preceding pointer is just a modifier, the following array is the subject, so it is an array.

Six, Wild hands

The wild pointer refers to a pointer that has no definite direction. The conditions that result in wild pointers are:

1. Pointer variables are created but not initialized.

2. After the pointer p is free or delete, it is not set to null.

Copyright notice: Thank the original author's hard work, Source: Bole Online columnist-time track, Link: http://blog.jobbole.com/102052/.

Interpreting pointers in C language

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.