Take the 2-minute tour ×

I am passing an array of int with 4 elements into a function and sizeof(array)/sizeof(array[0]) is saying I have fewer elements.

Here's example of what's happening:

main() {
  // declaring literal array
  int array[] = {1,2,3,5};

  // prints 16/4 (correct output for 4 elements)
  printf("%d/%d\n", sizeof(array), sizeof(array[0]));


  return 0;

function(int array[]) {
  // printing 8/4 instead of 16/4
share|improve this question
With all the edits, I wanna know (a) what happened to the original question, and (b) where is matrix_transposition_key declared?? The original question used array[] in the printf in function() this does not, and no longer even compiles. –  WhozCraig Feb 23 '13 at 22:00
You can't pass arrays to functions or return them from functions; they degrade to pointers to their first element. –  Ed S. Feb 23 '13 at 22:02
Sorry was rewriting some code to be generalized and copy/pasted the printf statements. I'll edit to fix –  Michael Johnston Feb 23 '13 at 22:09
add comment

closed as too localized by WhozCraig, Öö Tiib, Neolisk, spajce, agstudy Feb 24 '13 at 6:15


3 Answers

up vote 4 down vote accepted

The problem here is that function arguments that are arrays are transformed into pointers by the compiler. So it is as if your function had the signature

void function(int *array);

This means two things:

  1. You have to pass the size of the array explicitly as another parameter, since it has been "lost".
  2. sizeof(array) will only tell you what the size of a pointer is in your architecture. So by your output, we can tell that you are compiling for a 64-bit target but nothing else.

Note that sizeof(array[0]) is equivalent to sizeof(*array) (pointer arithmetic), so it will still give the correct value no matter what.

share|improve this answer
Thanks, I'll pass the size as a parameter. –  Michael Johnston Feb 23 '13 at 22:03
Also note, while this is very likely the problem, the posted code isn't using the parameter in its printf() anyway; its using the same unknown global that was used in main(). Now that would be a mystery if they printed different results. –  WhozCraig Feb 23 '13 at 22:04
add comment

The sizeof inside the function returns the sizeof pointer to your array which can is 4 or 8 depending on your system.

share|improve this answer
Or 2, if you're on a 16-bit system :) –  SecurityMatt Feb 23 '13 at 22:00
add comment

When you pass array as an I/P argument to a function, it is treated as a pointer containing its base address.

You are getting: sizeof(pointer)/sizeof(first element of array)

In GDB: (inside function() )

(gdb) pt matrix_transposition_key                                                               
type = int *                                                                                    
(gdb) pt matrix_transposition_key[0]                                                 
type = int 

(gdb) p sizeof(matrix_transposition_key)
$2 = 8                                                                                          
(gdb) p sizeof(matrix_transposition_key[0])                                                       
$3 = 4                                                                                          
share|improve this answer
add comment
