5

I understand how arrays decay to pointers. I understand that, for the compiler, this:

void foo(int *arg1);

is 100% equivalent to this:

void foo(int arg1[]);

Should one style be preferred over the other? I want to be consistent, but I'm having a hard time justifying either decision.

Although int main(int argc, char *argv[]) and int main(int argc, char **argv) are the same, the former seems to be much more common (correct me if I'm wrong).

1
  • 3
    If your goal is consistency, you should use the asterisk, since that's consistent with normal (non-parameter) declarations. The other syntax should die, it causes nothing but confusion. Commented Aug 26, 2012 at 4:34

2 Answers 2

7

I would recommend against using the [] syntax for function parameters.

The one argument in favour of using [] is that it implies, in a self-documenting way, that the pointer is expected to point to more than one thing. For example:

void swap(int *x, int *y)
double average(int vals[], int n)

But then why is char * always used for strings rather than char []? I'd rather be consistent and always use *.

Some people like to const everything they possibly can, including pass-by-value parameters. The syntax for that when using [] (available only in C99) is less intuitive and probably less well-known:

const char *const *const words vs. const char *const words[const]

Although I do consider that final const to be overkill, in any case.

Furthermore, the way that arrays decay is not completely intuitive. In particular, it is not applied recursively (char words[][] doesn't work). Especially when you start throwing in more indirection, the [] syntax just causes confusion. IMO it is better to always use pointer syntax rather than pretending that an array is passed as an argument.

More information: http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr#aryptrparam.

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

2 Comments

is it more safe to use[] this syntax then * one?
There is absolutely no difference to the compiler whether you use an array or a pointer as a function parameter—it will end up being a pointer either way, and consequently be subject to "pointer hazards".
3

Except for char*, I use Type array[N], where N is some number or a defined constant, when the passed item conceptually is an array (i.e., it contains N>1 elements), Type * pointer when the passed item is a pointer to exactly one object.

I tend to use std::vector if the array is of a variable size. C99's concept of variable sized arrays is not available in C++.

4 Comments

But the N is silently ignored, which I find misleading.
@KeithThompson: You are correct; the compiler does ignore it. However, you aren't just writing for the compiler, you are also writing for the human user of your code. That arrays decay into pointers is one of the biggest flaws in C/C++ from the perspective of a scientific programmer. There is no reason to kowtow to that huge gaping flaw.
What if you had a pointer to an array of strings (i.e. char pointer for the string, pointer to char pointer for words, pointer to pointer to char pointer to allow the function to write to it)? char *(*words[])? IMO char ***words looks better and is easier to take in at a glance. I suppose, though, that this kind of thing is more common in C (which I am primarily interested in) than C++, though.
@DavidHammen: Yes, you're writing for the human user, which is why I don't like to imply that the N actually means something.

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.