Two-level pointer and address operator

Source: Internet
Author: User
Tags numeric value

Next, we combine the following examples to understand the concept of level two pointers.

#include <stdio.h>

int main (void)
{
    int a = 3;

    int *one_ptr = &a; equivalent to int *one_ptr;one_ptr = &a;

    int **two_ptr = &one_ptr; equivalent to int **two_ptr; Two_ptr = &one_ptr;

    printf ("&a =%p, one_ptr =%p, *two_ptr =%p\n", &a, One_ptr, *two_ptr);
    printf ("&one_ptr =%p, *two_ptr =%p\n", &one_ptr, two_ptr);
    printf ("A =%d, *one_ptr =%d, **two_ptr =%d\n", A, *one_ptr, **two_ptr);

    return 0;
}

1, the definition and nature of the pointer

As in the 7th line of the example, it means declaring a pointer variable one_ptr and assigning the pointer variable a value of the address of the integer variable A.

Note: (1) the asterisk (*) is used only to indicate that the variable one_ptr is a pointer variable (it is important to note that one_ptr is the pointer variable, not *one_ptr), and (2) the asterisk (*) has three uses in the C language, the multiplication operator, The indirect operator and the pointer used in the declaration, the latter two usages are related to the pointer; (3) The data type keyword int here has nothing to do with the pointer variable itself (the pointer variable itself has a data type, but it has no relevant keyword in the C language to represent it. All we know is that the pointer variable is usually 4 bytes in a 32-bit machine, used to store a 32-bit byte address, and that only the pointer variable one_ptr can be used only to store the first address of an integer data (such as a), otherwise the type mismatch is warned.

Similarly, in the 9th line of the example, it means declaring a pointer variable two_ptr and assigning the pointer variable the first address of a data type variable such as one_ptr, such as int *.

In summary, in a pointer variable declaration, an asterisk (*) near the name of a pointer variable is the third use of the asterisk, and the other part is the data type of the variable that the pointer variable is declared to point to. For example, the pointer variable one_ptr the data type of the variable to point to is int, and the pointer variable two_ptr the data type of the variable to point to is an int * type. That is, int and * together determine all the characteristics of the variable one_ptr and have two layers of meaning, the asterisk (*) indicates that the variable one_ptr is a pointer variable, the keyword int represents the pointer variable one_ptr the data type of the variable to be pointed to is integral type, and similar, int * and * Collectively determines all the characteristics of the variable two_ptr.

2, the pointer variable address and value

In the C language, variables of any type have two basic attributes: address and value. The address is also a numeric value, in a 32-bit machine, the address is a 32-bit unsigned integer number. The address value of a variable is generally obtained by taking the address operator (&).

As in the example, the value of variable A is 3, the address is &a; variable one_ptr is &a, the address is &one_ptr; pointer variable two_ptr value is &one_ptr, address is &two_ PTR (this value is not printed in the example).

Example Results output:

&a = 0xbfac7f28, one_ptr = 0xbfac7f28, *two_ptr = 0xbfac7f28 &one_ptr
= 0xbfac7f24, *two_ptr =
0xbfac7f24 A = 3, *one_ptr = 3, **two_ptr = 3

*one_ptr and **two_ptr are equivalent to using an integer variable a,*two_ptr equivalent to using a pointer variable one_ptr.

Thinking: What the data type of the pointer variable is.

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.