2

Between formal parameters in a function definition, like:

void change (int *s)
{   
    s[0] = 42;
}

And another definition:

void change (int s[])
{   
    s[0] = 42;
}

They are the same I assume, as *(a+0) is the same as a[0].

Is there a reason to prefer one over the another? Please note, the preference pertains to coding style.

3 Answers 3

6

Yes, they are exactly the same. All function parameters declared as arrays are adjusted to the corresponding pointer type.

Personally, I prefer the former which actually makes it look like a pointer declaration which is what it is in both cases.

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

5 Comments

@AA: The pointer value is whatever you pass in. There is no array in the code that you have shown but you could pass in a pointer into an array of int or a pointer to a single int in either case.
The argument to this function is an array, I should have mentioned.
@AA: This may sound like nitpicking but the argument to the function cannot be an array as you cannot pass or return arrays to or from functions in C. In C, whenever you use the name of an array in an expression (except as the operand to unary & or sizeof operators), the name decays to a pointer to the first element of the array. If you try and pass an array to a function you automatically end up passing a pointer to the first element of the array but this is part of the interpretation of the calling expression and nothing to do with the declaration or definition of the called function.
@Charles: Good advice is !nitpicking :-). I understand what you mean, but if I call the function like: int x[] = {1, 2, 3, 4}; change(x), I am aware the name decays to a pointer to x[0], but won't we call it argument to the function is an array?
@AA: Informally, yes, you say that you are passing the array x to the function but the language mechanism by which this happens is that x is decays to &x[0] and this pointer is what is passed to the function. Inside the function the pointer can be used (in most cases) as if it was an array because pointer arithmetic and array access are handled the same in C.
4

I prefer the second one because it is then clear that you intend to use s as an array. (even though it is a pointer technically)

I think it is just another subjective thing though.

2 Comments

Yes, I agree it is subjective and argumentative even, but the point is when one is learning, we tend to look for the best way to do something. That is why I asked, what is the norm.
If you intend to receive a pointer to a int[] OR to a single int, then the first way is better. If you intend to only receive an array as the first argument to this function, I would say it is best to use the second way. What if someone (or you) passed a pointer to a single int and you performed operations on it expecting it was a int[]?
3

There is one case where they can be different, in the current C language. C99 allows the following:

void change (int s[static 2])
{   
    s[0] = 42;
}

where the [static 2] imposes a constraint on the interface of the function that the pointer passed must be such that s[0] and s[1] access valid objects of type int. In particular, the interface does not permit NULL pointers.

Note that [static 1] is a convenient way to specify simply that the pointer must point to a valid object.

Comments

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.