3

When we pass an array as an argument we accept it as a pointer, that is:

func(array);//In main I invoke the function array of type int and size 5

void func(int *arr)

or

void fun(int arr[])//As we know arr[] gets converted int *arr

Here the base address gets stored in arr.

But when the passed array is accepted in this manner:

void func(int arr[5])//Works fine.

Does the memory get allocated for arr[5]?

If yes, then what happens to it?

If no, why isn't the memory allocated?

1
  • there can't possibly be any new ground dealing with C arrays as parameters on SO... in looking to close as a duplicate... I can't because there are too many to choose from. Commented Aug 3, 2013 at 17:24

4 Answers 4

4

Does the memory gets allocated for arr[5]?

No, it doesn't.

If no,why memory is not allocated?

Because it's not necessary. The array, when passed to a function, always decays into a pointer. So, while arrays are not pointers and pointers are not arrays, in function arguments, the following pieces of code are equivalent:

T1 function(T2 *arg);
T1 function(T2 arg[]);
T1 function(T2 arg[N]);
Sign up to request clarification or add additional context in comments.

11 Comments

Memory should be allocated for arr[5] and it should have block scope.
@haccks As I stated, no, no memory is allocated at all if the declaration is in a function argument list.
Both are pushed on the stack, hence memory gets allocated. But in case of f(int x[5]) only memory for pointer variable gets reserved.
@haccks The reason: a design decision. If arrays didn't decay into pointers when passed to a function, then, due to C's pass-by-value nature, all the array would have to be copied. That's not the way to go.
@haccks Thanks. The "following pieces of code are equivalent" part of my answer implies (well, only as much as it's "implicit") this very fact.
|
3
void func(int *arr)
void func(int arr[])
void func(int arr[5])

are all equivalent in C.

C says a parameter of an array of type is adjusted to a pointer of type.

4 Comments

On the assembler level, you are right. On the language level, you are not: void func(int arr[5]) asserts that you pass an array of 5 elements, the others do not. I'm not entirely sure how compilers use this to detect errors, but at the very least sizeof(arr) will return 20 in the last case, not in the other ones.
@cmaster void func(int arr[5]) asserts that you pass an array of 5 elements This is wrong, this is equivalent to void func(int *arr). at the very least sizeof(arr) will return 20 in the last case This is also wrong, sizeof(arr) is equal to sizeof(int *) in all three functions.
@cmaster No, it won't return 20. It won't return sizeof(int [5]) at all. The cases ouah enumerated are really, truly equivalent. Function arguments are a special case - you simply can't have functions take array arguments. Arrays always decay to pointers when passed to a function, and the semantics of argument declarators match this fact. And here's the proof.
Ups, once again my language of choice surprises me... thanks for the link @H2CO3 ... Interestingly, the code even compiles with -Wall -pedantic when the passed array is too short for the declared argument, so my comment was as wrong as can be :-(
0

When you place an array size in a parameter declaration the number is totally ignored. In other words

void foo(int x[5]);

is exactly the same as

void foo(int *x);

Comments

0

Hope the following code/comments helps. When coding C the programmer must ensure that many items agree in various programs. This is both the fun and the bane of programming in C.

Note. Defining int arr[5] in a parameter list does NOT allocate storage for the data that is being passed. The declaration is valid and endorsed but only because it lets the compiler perform its type checking. Although, the compiler does allocate storage when a function is called that storage does not store YOUR data. You must allocate your data either through an explicit declaration (as in main in the following example) or you need to issue an malloc statement.

I ran the following code in Eclipse/Microsoft C compiler and NO statements were flagged with a warning or an error.

    //In main invoke the functions all referring the same array of type int and size 5

    void func1(int *arr)
    { // arr is a pointer to an int.  The int can be the first element
      // of an array or not.  The compiler has no way to tell what the truth is.
    }

    void func2(int arr[])
    { // arr is a pointer to an array of integers, the compiler can check this
      // by looking at the caller's code.  It was no way to check if the number
      // of entries in the array is correct.
    }

    void func3(int arr[5])
    { // arr is a pointer to an array of 5 integers.  The compiler can
      // check that it's length is 5, but if you choose to overrun the
      // array then the compiler won't stop you.  This is why func1 and func2
      // will work with arr as a pointer to an array of ints.
    }

    void main()
    {
      int data_array[5] = {2,3,5,7,11};

      func1(data_array);

      func2(data_array);

      func3(data_array);

    }

Hope this helps, please ask for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.