The difference between an array pointer and a pointer to the first element of a group

Source: Internet
Author: User

#include <stdio.h>void Main (){int a[5]={1,2,3,4,5};int *ptr= (int *) (&a+1);printf ("%d,%d", * (a+1), * (ptr-1));return;}output is: 2,5If int*ptr = (int*) (&a+1) is converted to int* ptr= (int *) (a+1) then the output is 2,1 Explanation:* (a+1) is actually very simple refers to a[1], the output is 2.The problem is the second point, what is the output of the * (PTR-1)? as explained below, &a+1 is not the first address +1, the system will consider adding an entire array of a, which offsets the size of the entire array a (that is, the size of the 5 int). so int *ptr= (int *) (&a+1), actually PTR is actually & (A[5]), which is a+5.What is the reason? &a represents a pointer to an entire array, its value is the same as the address value of the first element of a group, but the memory size that the pointer points to is the total size of the entire array. So &a+1 means the next memory cell of the last element of the entire array a starts the array pointer and then convert the pointer to a pointer to int, then Ptrl points to the 4 bytes (int*) memory that starts at the next memory cell after the last element of array A. &a is an array pointer with the type int (*) [5], and the pointer plus 1 to add a certain value according to the pointer type, the different type of pointer +1 after the increase in size, a is a length of 5 int array pointer, so to add 5*sizeof (int), so ptr is actually a [5], but the PTR and (&a+1) type is not the same, this is very important, so ptr-1 will only subtract sizeof (int*), A,&a address is the same, but the meaning is not the same, a is the first address of the array, that is, the address of a[0],& A is the first address of an object (array), a+1 is the address of the next element of the array, that is, a[1],&a+1 is the address of the next object, or A[5].  In addition, there is a similar written question:about int a[10]; Ask the following address which cannot represent a[1]?
A. a+sizeof (int)
B. &a[0]+1
C. (int*) &a+1
D. (int*) ((char*) &a+sizeof (int.))answer: A. a+sizeof (int) parsing:
A. a+sizeof (int)//incorrect, equivalent to pointer operation A + 4 on a 32-bit machine
B. &a[0]+1//correct, array first element address plus 1, according to pointer operation is a[1] address
C. (int*) &a+1//correct, the array address is coerced to int*, then add 1, so and b means a meaning
D. (int*) ((char*) &a+sizeof (int))//correct, the data address is first converted to char*, then add 4, according to the pointer operation formula, move forward 4 * sizeof (char), then be converted to int*, obviously a[1] The AddressThe ps:c Answer (int*) &a+1 is not the same as the (int *) (&a+1) in the above topic, (int*) &a+1 takes the address of the array and then forces the address of the group's first element to be added 1, so (int*) &a+1 can represent a[1] addresses, while (int *) (&a+1) refers to the address of the next array based on the above analysis.

The difference between an array pointer and a pointer to the first element of a group

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.