简体   繁体   English

函数调用中char []和char *之间的区别

[英]Difference between char[] and char* in function call

I have found myself unable to explain why the following piece of code works. 我发现自己无法解释以下代码为何起作用。 Needless to say, I am quite new to C++... 不用说,我对C ++还是很陌生。

#include <cstdio>

void foo(char* p)
{
    p[0] = 'y';
}

int main()
{
    char a[1];
    a[0] = 'x';
    printf("a[0] = %c\n", a[0]);
    foo(a);
    printf("a[0] = %c\n", a[0]);
    return 0;
}

This program outputs 该程序输出

a[0] = x
a[0] = y

What intrigues me us that I am not passing a pointer, but an array, to foo. 令我着迷的是,我没有将指针传递给foo,而是将数组传递给了foo。 So how can foo change the value of the array a? 那么foo如何改变数组a的值呢? Does this apply only to arrays of char? 这仅适用于char数组吗?

The answer to Difference between char and char[1] , confirms my observation, but it does not go into detail about why this is the case. char和char [1]之间的区别的答案证实了我的观察,但并未详细说明为什么会这样。

Thanks! 谢谢!

When you're passing an array to a function it decays to a pointer to the first element. 当您将数组传递给函数时,它会衰减为指向第一个元素的指针。

The following are completely equivalent: 以下是完全等效的:

void foo(char* p);
void foo(char p[]);
void foo(char p[42]); /* Or any other number. */ 

Does this apply only to arrays of char? 这仅适用于char数组吗?

It applies to any array. 它适用于任何数组。 I recommend the aryptr section of the C FAQ . 我推荐C FAQaryptr部分

In C, arrays, in most contexts (*), decay into a pointer to their first element. 在C语言中,在大多数情况下(*),数组都会衰减为指向其第一个元素的指针。

In your case, the array a decays into a pointer to a[0] . 在您的情况下,数组a衰减为指向a[0]的指针。

The array int arr[12][23] , when used just by its identifier, decays to a pointer to its first element, namely arr[0] , which is of type int (*)[23] (pointer to array of 23 ints). 数组int arr[12][23]仅由其标识符使用时,会衰减为其第一个元素的指针arr[0] ,其类型为int (*)[23] (指向23的数组的指针)整数)。

(*) Arrays do not decay when used as the argument to the sizeof operator, or when used as argument to the & operator or when used as initializer (a string literal) for a character array. (*)用作sizeof运算符的参数,用作&运算符的参数或用作字符数组的初始化程序(字符串文字)时,数组不会衰减。

When You say 当你说

char a[5];

the compiler allocates 5 sequential blocks of memory, and the address of the first block is assigned to a. 编译器分配5个连续的内存块,并且第一个块的地址分配给a。 So by definition name of the array (a in our case) is just a pointer to a memory location. 因此,根据定义,数组的名称(在我们的例子中为a)只是指向内存位置的指针。

Now when we say a[0], compiler manipulates it as *(a+0*sizeof(array datatype ie char, int etc.)), symbolically a[i] is represented as *(a+i*sizeof(array datatype ie char, int etc.)) 现在,当我们说a [0]时,编译器将其处理为*(a + 0 * sizeof(数组数据类型,即char,int等。)),符号地将a [i]表示为*(a + i * sizeof(array数据类型即char,int等))

As far as function parameter passing is concerned When we pass an array to a function basically it is just the address of the first element which is passed. 就函数参数传递而言,当我们将数组传递给函数时,基本上只是传递的第一个元素的地址。 For more details regarding this plz follow this link or read the @cnicutar's answer (as he has already posted a correct answer there is no point repeating that again)... 有关此plz的更多详细信息,请单击此链接或阅读@cnicutar的答案(由于他已经发布了正确的答案,因此没有必要再重复一次)...

You can try this to convince yourself how this works. 您可以尝试这样做以说服自己如何工作。

int a[8], *p;
p = a;
printf("%p, %p, %p\n", a, &a[0], p);

数组无非是一个指针(指向第一个元素),至少是为了传递参数。

The three prototypes here are equivalent: 这里的三个原型是等效的:

void foo(char *p);
void foo(char p[]);
void foo(char p[42]);

C says a declaration of parameter of type array of T is adjusted to a declaration of type pointer to T . Ç说的类型阵列的参数的声明T 调节到类型的指针到一个声明T

Array name points to the address of the first element of the array. 数组名称指向数组第一个元素的地址。 Please refer: Is an array name a pointer? 请参阅: 数组名称是指针吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM