tags:

views:

4547

answers:

8

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

+9  A: 

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

ojblass
A: 

Both are one and the same although second one looks nice and clarifies the intention of passing the array to the function. But how does the SomeFunction know the size of the array being passed, is it always assumed as 40 ? I feel it is better to pass the size also as the parameter to SomeFunction.

Naveen
I agree, you should pass the size as well as the array.
Jason Coco
Even better is to use a std::string, or boost/tr1::array.
GMan
I agree if it is a new code, but if the code is already written it may not be possible to change the datastructures so easily..
Naveen
Also, we have no idea if C++ is an option since the question said C array and the tag is simply C
Jason Coco
Oops, I left c++. I thought I was still in that section. Fading away now. *cough* pass the size
GMan
+1  A: 

These two variants are equivalent and passing like this

SomeFunction(tmpry);

looks cleaner.

sharptooth
+6  A: 

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.

Willi Ballenthin
+4  A: 

It may be significant, if SomeFunction is a macro and takes "sizeof" of one of its arguments, because sizeof(tmpry) may not necessarily be equal to sizeof(&tmpry[0]).

Otherwise, as others pointed out, they are exactly the same.

Alex B
In this case there is no macro involved. However how would a macro treat them differently? Doesn't a macro work like a string search and replace so the function it produced would be equivalent to a normal function anyway?
sipwiz
Results will differ if the macro takes sizeof(macro argument).
Alex B
#define Macro(x) RealFunctionCall(x,sizeof(x)) // For example
dreamlax
+1  A: 

As everyone else said, the two notations are equivalent.

I would normally use the simpler one, unless there are multiple calls like this:

SomeFunction(&tmparry[0]);
SomeFunction(&tmparry[10]);
SomeFunction(&tmparry[NNN]);

Where ideally all the constants (magic numbers) would be enum (or #define) constants.

Jonathan Leffler
hasen j
I disagree - but it is a subjective decision.
Jonathan Leffler
A: 

The declaration

int a[10]; int *pa;

There is one difference between an array and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; construction like a=pa and a++ are illegal

As format parameters in a function definition, char s[] and char *s are equivalent;

From: The C Programming Language 2th, Page 99-100

BianJiang
To quote from page 99: "When an array name is passed to a function, what is passed is the location of the initial element"
David
+1  A: 

The C Programming FAQ section on arrays and pointers tackles this (and many other common C questions and confusions.

mctylr