Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing C array as char* function parameter

Tags:

arrays

c

I've got some code I'm mantaining with the following variable declaration:

char tmpry[40];

It's being used with this function:

char *SomeFunction(char *tmpryP) {
   // Do stuff.
}

The function call is:

SomeFunction(&tmpry[0]);

I'm pretty damn sure that's just the same as:

SomeFunction(tmpry);

I've even checked that the char* pointer in SomeFunction ends up pointing to the same memory location as the array in both cases.

My question is a sanity check as to whether the two function calls are identical (and therefore the original programmer was just being nasty)?

like image 990
sipsorcery Avatar asked Apr 08 '09 04:04

sipsorcery


2 Answers

they are exactly the same.

someArray[i]

means exactly

*(someArray + i)

where someArray in the second case is a pointer to the memory location. Similarly,

&someArray[i]

means exactly

(someArray + i)

In both these cases the terms are pointers to memory locations.

like image 131
Willi Ballenthin Avatar answered Nov 20 '22 15:11

Willi Ballenthin


The two are equivalent and I think SomeFunction(tmpry); is more readable.

like image 33
ojblass Avatar answered Nov 20 '22 17:11

ojblass