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.

4
  • 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. Commented Oct 16, 2020 at 6: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. Commented Oct 16, 2020 at 7:03
  • 1
    Does this answer your question? Passing an array as an argument to a function in C Commented Oct 16, 2020 at 7:20
  • @GiovanniCerretani Yes. Commented Oct 16, 2020 at 7:32

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hello PawZaw, Yes it is acting as a pointer, but my question was that how it is handled in memory, or does it cause any memory corruption somehow.
It is not "acting as" a pointer. it "is" a pointer.
@eric True, that wasn't too precise of me, I'll edit it
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.