The substance of the C pointer

Source: Internet
Author: User
Tags constant integer

Recently on the C + + pointer particularly cold, on the detailed study of the next

Look at the following definition:

Char a[] = "Hello World";

Char *p = "Hello World";

1. What is an address

The address itself is a basic data type, which is the same as the basic types of integers, floating-point numbers, characters, and so on. The pointer is not a type, the real type is an address, and the pointer is simply a variable that stores the data type of the address.

For example, for

int i=10;

10 is an integer, and I is a variable that stores an integer, and the pointer is like this I, and the address is like that 10. The pointer is able to add and subtract because it is not a pointer, it is not a variable of the pointer, it is the nature of the address data type, because the address has the ability to add and subtract, so the pointer as the address of the variable can be added subtraction operation. This is the same as the integer variable because the integer can be subtraction so it can be subtraction a truth.

2. What is a pointer

The pointer itself is a variable, which is the same as the other variables. The pointer itself is a symbol.

For example, int *p, we define a pointer p, the compiler will allocate a 2 (32-bit system is 4 bytes) byte storage space in memory. When we use P, the value of P is what is stored in the storage space just allocated. To take a look at the variable, we define int a = 10,a is a variable name. When we use a, we are using the contents of the storage space corresponding to a. A itself can be understood to be a symbol, which corresponds to a storage space. &a is the address of the storage space corresponding to a. In this way, the pointer is also a variable, itself is a symbol, in the use of P, we use the contents of the corresponding storage space p, and the contents of this storage space is somewhat special, it is not an integer, not a floating-point number, but an address type of data. So what is the meaning of &p, &p is the address of the storage space that p corresponds to (this address is not the content of the storage space P). So how does *p understand it? Remember that when used, the value of P is the content of the corresponding storage space, then *p is naturally the content of the storage space corresponding to that address.

3. What is an array name

Array name: is a number, this number and an integer, similar to floating-point numbers, is an address type of data. Just like int a = 10; Here's 10 is an integer, which is a level with the address type data. That is, the array name is similar to the integer 10, and the gap with a is mostly not a concept. It should be said that the array name is the same as the &a, is an address data, and the array name is also a constant constant, address constant.

The area used to hold the array is a statically allocated memory (not static) in the stack, and the array name is the representation of this memory, which is defined as the first address of the memory. This shows that the array name is an address, and that it is a constant that cannot be modified, and, in full, an address constant. Array names, like enumerated constants, are symbolic constants. The symbol for the array name represents the first address of that memory. Watch out! The value of the symbol that is not the array name is the first address of that memory, but the array name is the symbol itself represents the address value of the first address, this is the address, which is the meaning of the array name belongs to the symbolic constants. Because the array name is a symbolic constant, so it is a right value, and the pointer, as a variable, but a left value, a right value will never be the left value, then, the array name will never be a pointer!

4. To discuss the difference between the character array and the string pointer that we defined at the beginning

We assume that these two definitions are in the same function (such as main).

Continued: * (a+1) =u;

* (p+1) = u

The first operation will pass, and the second operation will have a segment error. What's the reason? Then we need to understand the storage area problem in the program space allocation. C + + program corresponding storage area, divided into three categories

1. Static storage: When the program is compiled, it is already allocated, which exists throughout the running of the program. It mainly holds static data, global data and constants.

2. Stack area: When the function is executed, the storage units of local variables within the function can be created on the stack, and the storage units are automatically released when the function is finished. Stack memory allocation operations are placed within the processor's instruction set, which is highly efficient, but allocates a limited amount of memory.

3. Heap area: Also known as dynamic memory allocation. The program uses malloc or new to request memory of any size at run time, and the programmer is responsible for releasing the memory with free or delete at the appropriate time.

Here char a[] = "Hello World", the space is allocated in the stack, so you can modify the contents of it. While char *p = "Hello World", its space is allocated in the static storage area. And Hello World is a string constant and is not allowed to be modified.

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: 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.