Light
Light

Reputation: 149

Passing array to a function

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?

Upvotes: 3

Views: 280

Answers (4)

JackCColeman
JackCColeman

Reputation: 3807

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.

Upvotes: 0

user529758
user529758

Reputation:

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]);

Upvotes: 4

6502
6502

Reputation: 114481

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);

Upvotes: 0

ouah
ouah

Reputation: 145829

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.

Upvotes: 3

Related Questions