有什么作为函数参数的char海峡[]和char * str中之间的区别?

有什么作为函数参数的char海峡[]和char * str中之间的区别?

问题描述:

假设我们有以下函数原型:

Say we have the following function prototypes:

void function1(char str[]);
void function2(char *str);

现在说我们有一个字符串字符名称[] =约翰; ,我们希望通过这些功能。两者有什么区别?他们有什么用途和限制?是否有哪一个是preferred在其他情况?难道有所作为,如果不是字符串分别为的char *名称=约翰

Now say we have a string char name[] = "John"; that we wish to pass through these functions. What is the difference between the two? What are their uses and limitations? Are there circumstances in which one is preferred over the other? Would it make a difference if instead the string were initialized as char *name = "John"?

我了解使用字符海峡[] 的char * str中在函数中,不过我还是要说的区别知道他们作为函数的参数或参数的行为。

I understand the difference between using char str[] and char *str within a function, but I don't know their behavior as function parameters or arguments.

绝对没有什么区别用C

void function1(char str[]);
void function2(char *str);

由于字符海峡[] 只是减少了的char * 时,作为参数传递给function.And为记录,甚至字符海峡[20] 究竟同样的事情作为函数将其视为的char * str中

because char str[] simply reduces to char * when passed as argument to a function.And for the record, even char str[20] is exactly the same thing as the function sees it as char *str.

至于是否会有所作为,如果在字符串为

As for whether it would make a difference if the string were initialized as

char *name = "John";

是的,它!这个字符串约翰的以下地址被分配到指针名称,另一个地址可后reaassigned到名称。

yes,it does!Here address of that string John is being assigned to pointer name, and another addresses can be reaassigned to name later.

char *name="John";
name="Mary";  //Works in C

但在

char name[]="John";

要初始化字符数组对象名称约翰这里,差异是,你就是不能initialization.The以下后重新分配另一个字符串到名称是错误的C:

you are initializing a character array object name to John.The difference here is that you just can't reassign another string to name after initialization.The following is wrong in C:

char name[]="John";
name="Mary";// Wrong

虽然张贴问题,在论坛里搜索了一分钟,看看是否问题已经是你的问题的answered.The第一部分已被要求,并回答你似乎真的感到困惑的第二部分很好多个times.Since,我'已经回答了这里。

While posting questions,search the forum for a minute to see if the question has already been answered.The first part of your question has been asked and answered very well multiple times.Since you seemed genuinely confused about the second part,I've answered that here.