0
#include<stdio.h>
void length(int arr[])
{
 int len=sizeof(arr)/sizeof(arr[0]);
 printf("%d",len);
} 

main()
{
 int arr[]={4,5,6,3,21,9,3,8};
 length(arr);
}

In this code the answer is always 2. I am unable to understand why.Is it necessary to send the size always.

Debdipta Halder
  • 497
  • 1
  • 5
  • 19

3 Answers3

1

It is always 2 because arr is a pointer and you're on a 64-bit system. And int is 32 bits on your system. Divide them out, and you get 2.

You simply cannot get the length of an array like this, because it decays to a pointer when passed to a function. You need to pass the size explicitly, or use a sentinel to designate the end of the list (like null-terminated strings do).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

In C, you cannot pass whole array to the called function, because in that case it may take huge stack space, since, as far as I know, arguments passed to functions are stored on the stack.

Hence when you try to pass the array to the function, the array name decays into a pointer to first element of the array because array is stored in contiguous memory locations, and therefore you just need to know address of first element.

As already pointed out by @John Zwinck, the exact reason for you always getting output as 2 may be that you are on a 64-bit system.

On a 64-bit system/compiler the size of a pointer is 8 bytes (simply because the system can access 2^64 addresses and hence requires 8 bytes to store them) and size of int is 4 bytes.

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • Do you have a reference to back up the "because" claim? – juanchopanza Oct 10 '14 at 09:27
  • No it was my calculated guess. Because an array of (say) 100 statically allocated integers cannot be allocated anywhere in C's memory map other than in stack. Please comment if you think otherwise. – 0xF1 Oct 10 '14 at 14:33
  • I'd say it is up to you to provide proof for claims you make in your answers. It is plausible, but you are stating it as a fact. – juanchopanza Oct 10 '14 at 14:38
  • You are right. So, I have tried changing my wording. – 0xF1 Oct 10 '14 at 14:50
1

Yes, it's necessary to send the size always, sizeof(arr) reports the size of a pointer:

Q: Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? I have a test routine

f(char a[10]) { int i = sizeof(a); printf("%d\n", i); }

and it prints 4, not 10.

A: The compiler pretends that the array parameter was declared as a pointer (that is, in the example, as char *a; see question 6.4), and sizeof reports the size of the pointer. See also questions 1.24 and 7.28.

References: H&S Sec. 7.5.2 p. 195

http://c-faq.com/aryptr/aryparmsize.html

Or you can use an extra first element containing the size:

#include <stdio.h>

void length(int arr[])
{
    int len = arr[-1];

    printf("%d", len);
} 

int main(void)
{
    int arr[]={0,4,5,6,3,21,9,3,8}; /* Note first element = 0 */

    arr[0] = sizeof(arr) / sizeof(arr[0]) - 1;
    length(arr + 1);
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94