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).
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:But then why is
char *always used for strings rather thanchar []? I’d rather be consistent and always use*.Some people like to
consteverything 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 wordsvs.const char *const words[const]Although I do consider that final
constto 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.