3

I have this piece of code:

#include <stdio.h>

int max_number(int numbers_array[]);

int main(){
    int numbers_array[] = {10, 20, 30, 40, 50, 10, 60, 2500, 25555};
    printf("size: %d\n", sizeof(numbers_array));
    max_number(numbers_array);
    return 0;
 }

int max_number(int numbers_array[]){
    printf("size of array: %d\n", sizeof(numbers_array));
}

And the output is:

size: 36
size of array: 8

But the output should be the same right? Does anyone know what the problem is? Thank you very much.

Gilian Joosen
  • 486
  • 3
  • 21
  • 2
    No, it shouldn't. In main() where the array is declared, sizeof returns the size of the allocation: 36 bytes. Inside max_number, it has no idea how many bytes were allocated to the array since it was only passed a pointer, and sizeof just gives the size of the pointer (8 bytes). – Lee Daniel Crocker Aug 03 '15 at 22:53
  • There is a way to detect the size of the array, but it's better just to pass the size as an additional parameter. – Beta Aug 03 '15 at 22:55
  • the posted code fails to cleanly compile. 1) sizeof() returns a 'size_t', not an 'int' so both printf() statements need the format string corrected. 2) the max_number() function is declared to return an 'int' but has no return statement. Strongly suggest: when compiling, enable all the warnings (for gcc, at a minimum use: '-Wall -Wextra -pedantic') – user3629249 Aug 03 '15 at 23:04
  • @Beta: "There is a way to detect the size of the array" could you please elaborate? – too honest for this site Aug 03 '15 at 23:25
  • possible duplicate of [size of array in c](http://stackoverflow.com/questions/7545428/size-of-array-in-c) – Ben N Aug 04 '15 at 00:14
  • @Olaf: `template size_t arraySize(T(&)[N]){return(N);}` – Beta Aug 04 '15 at 00:21
  • 1
    @Beta: Impressive. How do you emulate templates in C? ;-) – too honest for this site Aug 04 '15 at 00:26
  • 1
    @Olaf [facepalm!] Never mind... – Beta Aug 04 '15 at 00:27

3 Answers3

2

Of course. The first is the size of the declared variable

int numbers_array[] = {10, 20, 30, 40, 50, 10, 60, 2500, 25555}

its size is well known even at compile time: 9 elements * 4 bytes each = 36 bytes

The second is the size of a pointer to an integer, 8 bytes (64 bits addressing)

The point is that the function has no way of knowing the size of its array parameter at any time. Since arrays are pointers, it will always and only see a pointer.

The following

int max_number(int numbers_array[])

is equivalent to

int max_number(int *numbers_array)
Pynchia
  • 10,996
  • 5
  • 34
  • 43
1

Array will decay to a pointer while using as a parameter to a function. So if you check the size of your array variable inside function, it will show the size of the pointer. So if you have a question, how you can find the size of the array, simplest answer is pass it as another variable to the function.

More info: http://c-faq.com/aryptr/aryptrparam.html

Steephen
  • 14,645
  • 7
  • 40
  • 47
1

No, it shouldn't. In main() where the array is declared, sizeof returns the size of the allocation: 36 bytes. Inside max_number, it has no idea how many bytes were allocated to the array since it was only passed a pointer, and sizeof just gives the size of the pointer (8 bytes).

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55