问题描述
您好,
如果我们的c代码如下所示,我们会收到错误,因为在
我们尝试使用stringManipulator函数在第二次调用函数时修改字符串文字
。我的问题是:在
stringManipulator函数中,有没有办法确定char *
是否指向可写内存空间到字符串文字?
当然,这个例子很简单,但是当
函数调用栈得到20个函数时,我们遇到了这样的问题!提前致谢!
int stringManipulator(char * str)
{
/ *查看此处以查看字符串是否可写? * /
str [0] =''A'';
str [1] =''B'';
}
int main()
{
char string_array [10] =" thisisok";
stringManipulator(string_array);
stringManipulator(" thisisbad"); / *不应该这样做* /
}
Hello,
If we have c code like what''s below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function. My question is: in the
stringManipulator function, is there any way to identify if the char*
is pointing to writable memory space to to a string literal?
Sure, the example is trivial, but we run into such problems when the
function call stack gets 20 functions deep! Thanks in advance!
int stringManipulator(char *str)
{
/* check here to see if string is writable? */
str[0] = ''A'';
str[1] = ''B'';
}
int main()
{
char string_array[10] = "thisisok";
stringManipulator(string_array);
stringManipulator("thisisbad"); /* shouldn''t do this */
}
推荐答案
如果函数必须修改指针指向的内容,
然后你不能把它声明为const,并且必须求助于
(喘气)自律以避免传递
a指向不可修改的对象。你还应该注意另外一件事:确保你只需要
传递指向至少两个长度的字符串
个字符(加上一个终结符)。例如
这将产生未定义的行为:
char a [] ="" ;;
stringManipulator(a);
最后,请注意该语言保留给实施
任何外部名称以''str'开头,后跟小写
字符。你的名字''stringManipulator''违反了这个规则。
-Mike
If the function must modify what the pointer points to,
then you can''t declare it as const, and must resort to
(gasp) self-discipline in order to refrain from passing
a pointer to a non-modifiable object. There''s also another
thing you should take care with: make sure that you only
pass a pointer to a string of at least a length of two
characters (plus one more for the terminator). E.g.
this will produce undefined behavior:
char a[] = "";
stringManipulator(a);
Finally, note that the language reserves to the implementation
any external names beginning with ''str'' followed by a lower case
character. Your name ''stringManipulator'' violates this rule.
-Mike
使用''const''限定符定义字符串文字。当你通过这个功能时,它会响一个
铃。
int stringManipulator(char * str)
{
/ *在这里查看字符串是否可写? * /
str [0] =''A'';
str [1] =''B'';
}
int main()
{
char string_array [10] =" thisisok";
stringManipulator(string_array);
stringManipulator((char const *)" thisisbad"); / *不应该这样做
(Awsome)* /
/ *或* /
char const * p =" thisisbad";
stringManipulator(p);
返回0;
}
你也可以在调试时激活编译器/ lint检查(比如gcc的
-Wwrite-strings)。
-
A +
Emmanuel Delahaye
Define the string literal with the ''const'' qualifier. It will ring a
bell when you pass it eo the function.
int stringManipulator(char *str)
{
/* check here to see if string is writable? */
str[0] = ''A'';
str[1] = ''B'';
}
int main()
{
char string_array[10] = "thisisok";
stringManipulator(string_array);
stringManipulator((char const *)"thisisbad"); /* shouldn''t do this
(Awsome) */
/* or */
char const * p = "thisisbad";
stringManipulator(p);
return 0;
}
You also can activate a compiler/lint checking (like gcc''s
-Wwrite-strings), at last at debug time.
--
A+
Emmanuel Delahaye
所以不要。实现这一目标的时间是你在写下
代码的时候。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
电子邮件:rjh在上面的域名(但显然放弃了www)
So don''t. The time to get this right is when you''re writing the calling
code.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
这篇关于如何识别指向字符串文字的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!