C函数参数中的数组语法与指针语法

时间:2022-05-22 11:12:51

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

我理解数组如何衰减到指针。我明白,对于编译器,这个:

void foo(int *arg1);

is 100% equivalent to this:

是100%相当于:

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

虽然int main(int argc,char * argv [])和int main(int argc,char ** argv)是相同的,但前者似乎更常见(如果我错了,请纠正我)。

2 个解决方案

#1


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 *.

但是为什么char *总是用于字符串而不是char []?我宁愿保持一致,也总是使用*。

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他们可能做的一切,包括传值参数。使用[]时的语法(仅在C99中可用)不太直观,可能不太为人所知:

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

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

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

虽然我确实认为最终的const是过度的,无论如何。

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.

此外,阵列衰减的方式并不完全直观。特别是,它不是递归应用的(char words [] []不起作用)。特别是当你开始抛出更多的间接时,[]语法只会引起混淆。 IMO最好总是使用指针语法,而不是假装数组作为参数传递。

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

更多信息:http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec = aaryptr #aryptrparam。

#2


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.

除了char *之外,我使用Type array [N],其中N是某个数字或定义的常量,当传递的项目在概念上是一个数组时(即,它包含N> 1个元素),当传递的项目为时,键入*指针指向一个对象的指针。

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++.

如果数组的大小可变,我倾向于使用std :: vector。 C99的可变大小数组的概念在C ++中不可用。

#1


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 *.

但是为什么char *总是用于字符串而不是char []?我宁愿保持一致,也总是使用*。

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他们可能做的一切,包括传值参数。使用[]时的语法(仅在C99中可用)不太直观,可能不太为人所知:

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

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

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

虽然我确实认为最终的const是过度的,无论如何。

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.

此外,阵列衰减的方式并不完全直观。特别是,它不是递归应用的(char words [] []不起作用)。特别是当你开始抛出更多的间接时,[]语法只会引起混淆。 IMO最好总是使用指针语法,而不是假装数组作为参数传递。

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

更多信息:http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec = aaryptr #aryptrparam。

#2


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.

除了char *之外,我使用Type array [N],其中N是某个数字或定义的常量,当传递的项目在概念上是一个数组时(即,它包含N> 1个元素),当传递的项目为时,键入*指针指向一个对象的指针。

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++.

如果数组的大小可变,我倾向于使用std :: vector。 C99的可变大小数组的概念在C ++中不可用。