Array syntax versus pointer syntax in C function parameters

I understand how arrays break up into pointers. I understand that for the compiler this is:

void foo(int *arg1);

100% equivalent:

void foo(int arg1[]);

If one style is preferable to another? I want to be consistent, but it's hard for me to find a solution.

Although int main(int argc, char *argv[])they int main(int argc, char **argv)match, the former seems more common (correct me if I am wrong).

+5
source share
2 answers

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

One argument in favor of use []is that it indicates in a self-documenting way that a pointer should point to several things. For instance:

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

char * , char []? *.

const , , pass-by-value. [] ( C99) , , :

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

, const , .

, . , (char words[][] ). , , [] . , , .

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

+7

char*, Type array[N], N - , (.. N > 1 ), Type * pointer, item - .

std::vector, . C99 ++.

+3

All Articles