为什么传递一个字符串字面量到一个char *参数只有有时一个编译器错误? [英] Why is passing a string literal into a char* argument only sometimes a compiler error?
问题描述
我在C和C ++程序中工作。我们以前编译时没有make-strings-writable选项。
I'm working in a C, and C++ program. We used to be compiling without the make-strings-writable option. But that was getting a bunch of warnings, so I turned it off.
然后我得到了一堆错误的形式无法将const char *转换为char *在函数foo的argmuent 3中。所以,我经历了,做了很多的修改,以解决这些。
Then I got a whole bunch of errors of the form "Cannot convert const char* to char* in argmuent 3 of function foo". So, I went through and made a whole lot of changes to fix those.
然而,今天,程序CRASHED,因为字面值正在传递到一个函数,期望一个char *,并将第0个字符设置为0。没有做任何坏事,只是试图编辑一个常数,并崩溃。
However, today, the program CRASHED because the literal "" was getting passed into a function that was expecting a char*, and was setting the 0th character to 0. It wasn't doing anything bad, just trying to edit a constant, and crashing.
我的问题是,为什么不是编译器错误?
My question is, why wasn't that a compiler error?
如果有问题,这是在使用gcc-4.0编译的mac上。
In case it matters, this was on a mac compiled with gcc-4.0.
:
char * host = FindArgDefault("EMailLinkHost", "");
stripCRLF(linkHost, '\n');
其中:
char *FindArgDefault(char *argName, char *defVal)
{// simplified
char * val = defVal;
return(val);
}
和
void stripCRLF(char *str, char delim)
{
char *p, *q;
for (p = q = str; *p; ++p) {
if (*p == 0xd || *p == 0xa) {
if (p[1] == (*p ^ 7)) ++p;
if (delim == -1) *p = delim;
}
*q++ = *p;
}
*q = 0; // DIES HERE
}
编译并运行,直到尝试将* q设置为0 ...
This compiled and ran until it tried to set *q to 0...
编辑2:
大多数人似乎都错过了我的问题。我知道为什么char foo [] =bar的作品。我知道为什么char * foo =bar;不工作。
Most people seem to be missing the point of my question. I know why char foo[] = "bar" works. I know why char * foo = "bar"; doesn't work.
我的问题主要是传递参数。对我来说,一件事是这可能是一个C vs C ++问题?因为我有一些.c文件和一些.cpp文件,很可能C允许它,但C ++不...反之亦然...
My question is mostly with respect to passing parameters. One thing that occures to me is "Is it possible that this is a C vs C++ issue?" because I have some .c files and some .cpp files, and it's quite possible that C allows it, but C++ doesn't... or vice versa...
推荐答案
标准规定了一个特殊的规则,允许文字转换到 char *
/ code> qualification。 (4.2 / 2):
The standard specifies a special rule allowing the literal-to-char*
conversion which quietly drops const
qualification. (4.2/2):
不是宽字符串文字的字符串文字(2.13.4)可以转换为类型指针指向char;一个宽字符串文字可以转换为类型指向wchar_t的指针的右值。在任一情况下,结果都是指向数组的第一个元素的指针。仅当存在明确的适当的指针目标类型时才考虑此转换,而不是在通常需要从左值转换为右值时。 [注意:此转换已弃用。请参阅附录D.]
A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type "pointer to char"; a wide string literal can be converted to an rvalue of type "pointer to wchar_t". In either case, the result is a pointer to the first element of the array. This conversion is considered only when there is an explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an rvalue. [Note: this conversion is deprecated. See Annex D. ]
C ++ 0x标准进一步废弃了这个废话规则,完全从即将到来的标准中删除。
The C++0x standard takes that deprecation further… this nonsense rule is removed entirely from the upcoming standard.
错误必须包含 const char *
至 char *
是首先将文字转换为 const char *
的结果。
The const char*
to char*
error must be a result of converting a literal to a const char*
first.
这篇关于为什么传递一个字符串字面量到一个char *参数只有有时一个编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!