Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array size without sizeof operator

Tags:

I am trying to understand program below, but it's not clear to me.

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

Above code output is :

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

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

For reference I also tried printing (&a)[1] and a values which you can see in code. I tried also debugging.

like image 669
Harish Avatar asked Jun 02 '16 05:06

Harish


People also ask

How do you find the size of an array without using sizeof?

&a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing to *(&a + 1) gives the address after the end of the last element. *(a+1)-a => Subtract the pointer to the first element to get the length of the array. Print the size.

How do you read an array without knowing its size?

You can use String to take input and then convert/use it using Integer. parseInt() . Using this you don't have to allocate an oversized array.

How do you find the size of an array?

To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.


1 Answers

If you cast (&a)[1] and a to long before the calculation, then you will get your expected result. As haccks commented, you're currently calculating the pointer difference.

// These two sizes will be the same printf("sizeof array is %ld\n",sizeof(a)); printf("size of array using logic is %ld\n",((long)(&a)[1]-(long)a)); 

Explaining the Math

What's happening in this case, is &a is considered to be type int(*)[7].

Then, you reference (&a)[1], which translates to *((&a)+1). In English, this means "give me the point in memory 1 after the beginning of a." As &a happens to be type int(*)[7], that point is at the end of the array.

When you subtract a, the pointer to the beginning of the array, you are performing pointer arithmetic and take a base the size of an int (because a is an int array). So the expression ((&a)[1]-a) is counting how many int are between (&a)[1] and a.

An overview on pointer arithmetic can be found here.

like image 150
Carter Avatar answered Sep 22 '22 08:09

Carter