On pointers and Arrays in C (III.)

Source: Internet
Author: User

Original Reprint Address: http://see.xidian.edu.cn/cpp/html/475.html

On the basis of the original text to add their own ideas as a modification

Many beginners can't figure out what kind of relationship the pointers and arrays really have. I'll tell you right now: There's nothing between them! It's just that they often wear similar clothes to amuse you.

Pointers are pointers, and pointer variables, under 32-bit systems, always account for 4 bytes, which is the address of one memory. The pointer can point to anywhere, but not anywhere you can access it through this pointer variable.

An array is an array whose size is related to the type and number of elements. When you define an array, you must specify the type and number of its elements. Arrays can store any type of data, but cannot store functions.

Since there is no relationship between them, why do many people confuse arrays with pointers? Even many people think that pointers and arrays are the same. This is related to the C language in the market, there is hardly a book to explain the problem thoroughly, understand.

Access in the form of pointers and in the following form

Let's discuss in detail some of the specious features of these. For example, inside a function is defined as follows:
A
Char *p = "abcdef";
B
Char a[] = "123456";

1. Access pointers in the form of pointers to access and the following headers
Example a) defines a pointer variable p, whichitself occupies 4 bytes on the stack, and P stores the first address of a piece of memory. There is a static zone within this block, with a space size of 7 byte and no name for this memory. Access to this block of memory is completely anonymous .

For example, now we need to read the character ' E ', we have two ways:
1)
In the form of a pointer: * (P+4). First take out the address value stored in p, assuming 0x0012ff38, and then add a 4-character offset, get the new address 0x0012ff3c. Then remove the value on the 0X0012FF3C address.

2)
In the following form: P[4]. The compiler always resolves operations in the form of the following as pointers. P[4] This operation will be resolved to: Take out the address value stored in P, then add the offset of 4 elements in the brackets, calculate the new address, and then remove the value from the new address. In other words, the following forms of access are essentially no different from the way they are accessed in the form of pointers, but differ in their wording.

2. Access the array in the form of pointers and the following headers
Example B) defines an array of a,a elements with 7 char types, with a space size of 7. The array a itself is above the stack. Access to the elements of a must first find the first address of the first element of the array based on the name a of the array, and then find the corresponding value based on the offset. This is a typical " named + anonymous " access.

For example, now we need to read the character ' 5 ', we have two ways:
1)
In the form of a pointer: * (A+4). A This represents the first address of the first element of the array (which represents the first address of the first element, not the address of the first element), assuming 0x0000ff00, and then adding a 4-character offset to get the new address 0x0000ff04. Then remove the value on the 0x0000ff04 address.

2)
In the following form: A[4]. The compiler always resolves operations in the form of the following as pointers. A[4] This action will be parsed to: A as the first address of the first element of the array, then add the offset of 4 elements in the brackets, calculate the new address, and then remove the value from the new address.

From the above analysis, we can see that pointers and arrays are essentially two things that are completely different. Only they can be accessed "in the form of pointers" or "in the following form." One is full anonymous access, and the other is a typical named + Anonymous access. It is important to note that this "access in the form of XXX" is the way of expression.

Another thing to emphasize is that the above-mentioned offset of 4 represents 4 elements, not 4 bytes. It just happens to be a char. Type data 1 characters in size is 1 byte. Remember that this offset is the number of elements, not the number of bytes, and don't make a mistake when calculating the new address.

On pointers and Arrays in C (III.)

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.