c - Array size without sizeof operator -


i trying understand program below, it's not clear me.

    #include<stdio.h>     int main()     {         int a[]={1,2,3,4,5,6,9};         printf("sizeof array %d\n",sizeof(a));         printf("size of array using logic %d\n",((&a)[1]-a));         printf("value of (&a)[1] %p \n",(&a)[1]);         printf("value of %p \n",a);         printf("address of a[0] %p\n",&a[0]);         printf("address of a[1] %p\n",&a[1]);         printf("address of a[2] %p\n",&a[2]);         printf("address of a[3] %p\n",&a[3]);         printf("address of a[4] %p\n",&a[4]);         printf("address of a[5] %p\n",&a[5]);         printf("address of a[6] %p\n",&a[6]);     } 

above code output :

    sizeof array 28     size of array using logic 7     value of (&a)[1] 0x7ffc4888e78c      value of 0x7ffc4888e770      address of a[0] 0x7ffc4888e770     address of a[1] 0x7ffc4888e774     address of a[2] 0x7ffc4888e778     address of a[3] 0x7ffc4888e77c     address of a[4] 0x7ffc4888e780     address of a[5] 0x7ffc4888e784     address of a[6] 0x7ffc4888e788 

it's not clear me why ((&a)[1]-a)) on second print statement returning 7; should 0x7ffc4888e78c - 0x7ffc4888e770 0x1c i.e 28 total size of array.

for reference tried printing (&a)[1] , values can see in code. tried debugging.

so pointers not integers. sure, can convert them integers casting them integer type, or add integers them slide them around. not integers.

pointers mathematical vectors on integers, if have done linear algebra.

p1-p2 distance between p1 , p2, integer required add p2 reach p1.

when add integer pointer, have pay attention type of pointer. if pointer object of size 4, each time add 1 pointer numerical address increases 4, not 1.

the same thing true when subtract 2 pointers.

the key part here numerical value of address in memory matters, the type matters much understand happens.

the second strange thing going on here arrays decay pointers first element @ drop of hat. they, however, not pointers first element, convert them easily.

so when this:

(&a)[1] 

we taking address of a. address of a pointer of type int(*)[7]. pointer array, not pointer first element of array. difference in type of pointer. , 7 important.

we use [] on pointer. if have pointer or array p , value v, p[v] defined *(p+v). leads humor if v[p], isn't important.

let pa represent (&a). pa[1] going *(pa + 1).

now, pa pointer-to-an-array (not pointer-to-the-first-element of array). +1 adds full size of array (sizeof(int)*7) numeric value.

so pa+1 pointer one-past-the-end of a, , of type pointer-to-array.

we dereference, , non-existent array of size 7 right after end of array a.

then subtract a.

(&a)[1]-a 

this pointer decay kicks in. there no - operation on arrays, there - operation on pointers. c language helpfully decays each of these arrays pointers first element.

the pointer first element of a &a[0].

the pointer first element of array of size 7 after end of a ... &a[7].

both of these pointers of type int*. when subtract 2 int*s, numeric pointer value, divided sizeof(int). in case, easy -- 7.

this might easier if looked @ this:

(&a)[1]-(&a)[0] 

or

*(&a+1)-*(&a+0) 

&a pointer array a of type "pointer array of size 7". add 1 it, getting pointer array afterwards in 1 case, , 0 in other case.

we go down being arrays, , subtract. subtraction triggers decay pointer-to-first-element, pointer element right after end of a, , pointer first element of a.

&a[7]-&a[0] 

which is

&*(a+7)-&*(a+0) 

now &* nothing things pointers (which @ point), so:

(a+7)-(a+0) 

the question becomes, how have add a+0 reach a+7. answer, not surprisingly, 7:

(a+7) = (a+0)+7 

and displayed.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo