7

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

3
  • I think it would be good to pass a length also to SomeFunction, just in case... :) Commented Apr 8, 2009 at 6:25
  • 1
    The name of the character array is an alias for the address of the first element of the array. Commented Jul 15, 2009 at 16:07
  • Hello, I'm one of those "nasty" programmers who writes like this. Generally, I do this in C++ code, for the reason of lesser coupling to the array abstraction used at the moment. I.e. for an abstract array-like data structure (like std::vector<char>) with overloaded operator [] expressions x and &x[0] will get to mean very different things, and the second one is the one which fits better for my intent. Off course, all this doesn't matter so much for plain C, since you get almost none data abstraction in C anyway; thus the spelling with superior readability should be preferred. Commented Aug 20, 2013 at 11:08

8 Answers 8

12

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.

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

Comments

12

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

Comments

7

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.

3 Comments

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?
Results will differ if the macro takes sizeof(macro argument).
#define Macro(x) RealFunctionCall(x,sizeof(x)) // For example
5

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

Comments

3

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.

2 Comments

well, even then, tmparry + 10 is better than &tmparray[10]
I disagree - but it is a subjective decision.
1

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.

3 Comments

I agree, you should pass the size as well as the array.
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..
Also, we have no idea if C++ is an option since the question said C array and the tag is simply C
1

These two variants are equivalent and passing like this

SomeFunction(tmpry);

looks cleaner.

Comments

1

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.