0

I was studying memory layers on embedded systems and one question came to my mind. If the function parameter is a pointer, then one-word length area is occupied in the stack for sure. But what is happening when the function parameter is an array with not fixed size? for example,

void test1(uint32_t *pData)
{

}

void test2(uint32_t arr[])
{

}

Both functions given above gives same result in gcc 9.2 Compiler explorer and says that it is considered like pointer, but does not give any clue about where it is allocated or how it is handled. Any idea is welcomed.

Emre İriş
  • 77
  • 1
  • 5
  • 1
    In a parameter list of a function `uint32_t arr[]` is syntactic sugar for `uint32_t *arr`. Both are pointer for the function and can be an array or pointer on the caller side. – mch Oct 16 '20 at 06:57
  • @mch Then It is just a way to increase readability, on the test2 function it is okay to give address instead of an array starting address. – Emre İriş Oct 16 '20 at 07:03
  • 1
    Does this answer your question? [Passing an array as an argument to a function in C](https://stackoverflow.com/questions/6567742/passing-an-array-as-an-argument-to-a-function-in-c) – Giovanni Cerretani Oct 16 '20 at 07:20
  • @GiovanniCerretani Yes. – Emre İriş Oct 16 '20 at 07:32

2 Answers2

2

Your array parameter arr[] is actually a pointer (see below), so they are in fact the same thing.

See here: Passing an array as an argument to a function in C

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
PawZaw
  • 1,033
  • 7
  • 15
0

It does not cause any memory corruption, as the above answers say, it is syntactically the same thing, when you declare an array you can get its memory direction (pointer), and you pass a copy of that pointer to the function so it can point to the same place. C program allocates storage for every variable declared statically at compile time, when you call a function then what's happening in the background is that the program is asking for new memory space to the operating system, thus allocating new space for the local variables and parameters of the function.

Roman
  • 161
  • 7