C语言 函数参数中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17111140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String in function parameter
提问by Jeyaram
int main()
{
char *x = "HelloWorld";
char y[] = "HelloWorld";
x[0] = 'Z';
//y[0] = 'M';
return 0;
}
In the above program, HelloWorldwill be in read-only section(i.e string table). xwill be pointing to that read-only section, so trying to modify that values will be undefined behavior.
在上面的程序中,HelloWorld将处于只读部分(即字符串表)。x将指向该只读部分,因此尝试修改该值将是未定义的行为。
But ywill be allocated in stack and HelloWorldwill be copied to that memory. so modifying y will works fine. String literals: pointer vs. char array
但y将在堆栈中分配并HelloWorld复制到该内存中。所以修改 y 会正常工作。字符串文字:指针与字符数组
Here is my Question:
这是我的问题:
In the following program, both char *arrand char arr[]causes segmentation fault if the content is modified.
在下面的程序中,如果内容被修改,char *arr和char arr[]都会导致分段错误。
void function(char arr[])
//void function(char *arr)
{
arr[0] = 'X';
}
int main()
{
function("MyString");
return 0;
}
- How it differs in the function parameter context?
- No memory will be allocated for function parameters??
- 它在函数参数上下文中有何不同?
- 不会为函数参数分配内存??
Please share your knowledge.
请分享您的知识。
回答by Jonathan Leffler
Inside the function parameter list, char arr[]is absolutely equivalent to char *arr, so the pair of definitions and the pair of declarations are equivalent.
在函数参数列表中,char arr[]绝对等价于char *arr,所以定义对和声明对是等价的。
void function(char arr[]) { ... }
void function(char *arr) { ... }
void function(char arr[]);
void function(char *arr);
The issue is the calling context. You provided a string literal to the function; string literals may not be modified; your function attempted to modify the string literal it was given; your program invoked undefined behaviour and crashed. All completely kosher.
问题是调用上下文。您为该函数提供了一个字符串文字;字符串文字不能被修改;您的函数试图修改给定的字符串文字;您的程序调用了未定义的行为并崩溃了。一切都完全是犹太洁食。
Treat string literals as if they were static const char literal[] = "string literal";and do not attempt to modify them.
像对待字符串一样对待字符串文字static const char literal[] = "string literal";,不要尝试修改它们。
回答by ouah
function("MyString");
is similar to
类似于
char *s = "MyString";
function(s);
"MyString"is in both cases a string literal and in both cases the string is unmodifiable.
"MyString"在这两种情况下都是字符串文字,并且在这两种情况下字符串都是不可修改的。
function("MyString");
passes the address of a string literal to functionas an argument.
将字符串文字的地址function作为参数传递给。
回答by Nitin Singh
char *arr; above statement implies that arr is a character pointer and it can point to either one character or strings of character
字符 * arr; 上面的语句暗示 arr 是一个字符指针,它可以指向一个字符或字符串
& char arr[]; above statement implies that arr is strings of character and can store as many characters as possible or even one but will always count on '\0' character hence making it a string ( e.g. char arr[]= "a" is similar to char arr[]={'a','\0'} )
& 字符 arr[]; 上面的语句暗示 arr 是字符串,可以存储尽可能多的字符,甚至可以存储一个,但总是依赖 '\0' 字符,因此使其成为字符串(例如 char arr[]= "a" 类似于 char arr []={'a','\0'} )
But when used as parameters in called function, the string passed is stored character by character in formal arguments making no difference.
但是当用作被调用函数中的参数时,传递的字符串在形式参数中逐个字符地存储,没有任何区别。

