foo(char *s)
foo(char *s[])
foo(char s[])
所有这些的区别是什么?
是否有任何方法可以修改作为参数传递的数组中的元素,就像我们使用&
传递int
或float
并且实际参数的值被修改一样?
foo(char *s)
foo(char *s[])
foo(char s[])
所有这些的区别是什么?
是否有任何方法可以修改作为参数传递的数组中的元素,就像我们使用&
传递int
或float
并且实际参数的值被修改一样?
foo(char* s)
和 foo(char s[])
是完全等价的。在这两种情况下,您都使用数组的名称来传递它:char array[4];
foo(array); // regardless of whether foo accepts a char* or a char[]
在这两种情况下,数组都被转换为指向其第一个元素的指针。
指向数组的指针解决方案较少见。它需要以这种方式进行原型设计(注意*s
周围的括号):
void foo(char (*s)[]);
没有括号,你就在请求一个字符指针的数组。
在这种情况下,为了调用函数,你需要传递数组的地址:
foo(&array);
每次访问数组元素时,您还需要取消引用来自foo
的指针:
void foo(char (*s)[])
{
char c = (*s)[3];
}
就像这样,它并不是特别方便。然而,它是唯一允许您指定数组长度的形式,您可能会发现它很有用。这是我个人最喜欢的之一。
void foo(char (*s)[4]);
如果您尝试传递的数组没有恰好4个字符,则编译器将警告您。此外,sizeof
仍按预期工作。(显而易见的缺点是数组必须具有精确数量的元素。)
void foo(size_t n,char s [n])
。这仍然被简化为对第一个元素的指针,因为C不区分常规数组和VLA的“参数衰减”。 - Lundin foo(char *s)
并且
foo(char s[])
considered被认为是等价的。它们都期望使用char
数组进行调用。
另一方面,foo(char *s[ ])
不同,因为它需要一个char *
数组的地址。
memcpy()
/strcpy()
是可选项。 - Sourav Ghoshfoo(char *s)
is pretty straight-forward, it is just a pointer, which could potentially be a pointer to the first element of an array.
foo(char s[])
is a declaration of an array of char, but because of a (stupid) rule in the C standard, this gets translated to a pointer to char. Similarly, if you declare any array size, you still get a pointer to char.
Mentioned rule can be found in C11 6.7.6.3 Function declarators:
A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’
It would perhaps have been much better if this strange, confusing "would-be-array" syntax was banned entirely, as it is 100% superfluous and fills no other purpose but to confuse beginners who try to join the "C club". But then C was never a rational language...
Some will say that the the rationale is that you should never be allowed to pass arrays by value in C, for performance reasons. Well, tough luck, because you still can, despite this rule.
You can "hack the C standard" and pass an array by value:
typedef struct
{
char array[10];
} by_val_t;
foo (by_val_t arr); // bad practice but possible
Now there's really never a reason why you would want to pass an array by value, this is very bad program design and doesn't make any sense. I merely included this example here to prove that the rule in 6.7.6.3 completely lacks any rationale.
You could pass an array pointer as a parameter to a function. This is where things turn advanced, as this is quite obscure and has very limited practical use:
foo(char(*s)[])
If you specify an array size here, you actually get a bit of added type safety because compilers tend to warn about casts between array pointers of different size.
But note that array pointers are mainly just there to make the C language more consistent. There's very few reasons why you would ever want to pass an array pointer to a function (here is one somewhat complex example where I apparently found a valid use for it).
foo(char *s)
,foo(char *s[])
,foo(char s[])
,它们之间有什么区别?
在这些函数中,foo(char *s)
和 foo(char s[])
都将期望作为参数的 char 数组
。
除此之外,foo(char *s[])
将期望一个指向 char 类型的指针数组作为参数。
char *s[]
不是一个字符数组。 - ameyCUchar (*s)[]
是指向字符数组的指针。char *s[]
是字符指针的数组。 - zneak