How do pointers in the C language work?

Source: Internet
Author: User

We sometimes see this hungry code:

int *ptr = &x;

Here, PTR is a pointer to an X in-memory address.

Let's say that there is another statement like this:

int **ptr2 = &ptr;

We define a pointer to a pointer.

Suppose our computer is 8bit and the address is 8bit (and therefore only 256 bytes of memory). Represents a portion of memory (a row of numbers above represents an address).

  45   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69

+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

|    | 58 |    |    | 63 |    | 55 |    |    | h  | e  | l  | l  | o  | \0 |    |

+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

From the above you can see that address 63 is where the string "Hello" begins. In this case, suppose there is only one place in memory where "hello" is present, then:

Const Char " Hello ";

Define C as a pointer to the string "Hello", then the value inside C is 63. The pointer c itself is stored somewhere in the memory, and in the example above you can see where c is stored in 58. Now that we can define pointers to characters, we can define pointers to pointers:

Const char **CP = &c;

The CP now points to the address (58) where c is stored in the C,CP.

Can even be further:

Const char ***cpp = &cp;

CPP memory is the address of the CP, which is 55 (in the example above), the CPP itself is stored in the 60 location.

Why do we need these pointers?

    • The array name is usually the address of the first element of the array. If the data type stored in the array is T, the type of the logarithmic group reference is * t. Suppose there is an array of arrays of type T (two-dimensional arrays), naturally, the reference type of the two-dimensional array is * (*t) =**t, which is the pointer to the pointer.
    • Even if the array of strings looks like one-dimensional, it is actually two-dimensional because the string is an array of characters. Therefore, the type is char * *.

How does a pointer pointer work in the

C language?

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.