Pointers and Arrays

Source: Internet
Author: User
Tags arrays

Pointers and arrays are not the same, but "many times" we always think pointers and arrays are equivalent. Admittedly, the two can be replaced in some cases, but they cannot be considered appropriate in all cases. The pointers and arrays are not the same thing. The series will step through the differences between pointers and arrays and explain when an exponential group is equivalent to a pointer. This article belongs to one of the series of articles that are not a matter of pointers and arrays.

What is the nature of pointers and arrays, which is the focus of this discussion. From the memory structure point of view, the two are completely different concepts. declaration and definition of an array

The statement in C is described in detail in the article "declaring that matter". The definition and declaration of the array are then analyzed. Take the following declaration code as an example:

1 Char str[10];
2 extern Char str[];

The first declaration statement defines an array of type char and assigns it a memory space of 100 bytes in size. The second statement is to tell the compiler the type and size of the array. Because an array is not allocated memory space in an external reference declaration, this declaration does not need to specify the size of the array. For multidimensional arrays, you do not need to specify the size of the first dimension. memory layout of pointers

A pointer is essentially a memory address that binds it to an identifier in order to make it easier to use the memory address. Like what:

1 int i = 10;
2 int *p = &i; /* Assume the memory address of the variable i is 0x000f3e00 */

The above statement binds the address 0x000f3e00 and P together, and once the binding can no longer be modified, p is also called a pointer variable, or pointer. The variable in the pointer variable does not mean that P can be bound to another address, but instead emphasizes that the content in this address bound to P is mutable, that is, the value of I can vary. (wrong here, pointers can point to other content, not immutable)

Since pointer P is a memory address, the memory size of pointer p in a 32-bit system is always 4 bytes. Although the integer variable I also accounts for 4 bytes, the two same-sized memory spaces are fundamentally different. The pointer p can only hold memory addresses, and this address can only be the first address of the integer data. Even if other data is stored within p, it is treated as a memory address.

The following figure provides a closer look at the relationship of the pointer and the data it refers to:


As you can tell from the graph, the pointer itself occupies only 4 bytes, regardless of the amount of memory space the pointer refers to. Because the pointer p itself occupies 4 bytes of memory space, this part of the memory space will inevitably have a first address. The first address of the pointer P can be obtained by &p operation, which is the first address of memory space where pointer p is stored. As you can see from the above figure, the pointer p is the first address of the integer variable i, so we also call p a pointer to the integer variable i. memory layout of an array

An array is a contiguous amount of memory space in which the name of the memory space is the array name. Like what:

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