为什么不能为指针赋值? [英] Why can't I assign values to pointers?
问题描述
阅读常见问题和其他一切之后,我仍然感到困惑。如果我有一个以这种方式初始化的字符指针:
After reading the faq's and everything else I can find, I'm still confused. If I have a char pointer that is initialised in this fashion:
char * s =Hello world!
字符串在只读内存中,我不能这样更改:
The string is in read-only memory and I cannot change it like this:
*s = 'W';
以使Wello world!这我明白,但我不能,对我的生活,了解如何使它不是只读。我必须使用数组而不是指针吗?像这里?
to make "Wello world!". This I understand, but I can't, for the life of me, understand how to make it NOT read-only. Do I have to use an array instead of a pointer? Like here?
这是我的代码:
char *s = str;
char *e = s;
while (*e != '\0')
e++;
e--;
char *temp;
//Swop the string around
while (s <= e) {
*temp = *s;
*s = *e;
*e = *temp;
e--;
s++;
}
错误消息只是一个分段错误。如果这是一个真的愚蠢的问题,提前道歉。
The error message is just a segmentation fault. Apologies in advance if this is a really stupid question.
非常感谢所有的帮助。完成所有的建议后,我得到这个:
Thanks so much for all the help. After taking all your advice, I get this:
void something(char * str) {
char *store = str;
char *s = new char[strlen(str) + 1]; //Allocate memory. Nice one.
strcpy(s, str);
char *e = new char[strlen(str) + 1];
strcpy(e, str);
while (*e != '\0')
e++;
e--;
char temp; //no longer a pointer
while (s <= e) {
cout << *e;
temp = *s;
*s = *e;
*e = temp;
e--;
s++;
}
delete [] e;
delete [] s;
}
但是,函数结尾处的删除似乎是导致自己的分割错误。为什么?
however, the deletes at the end of the function seem to be causing their own segmentation faults. Why?
为了感兴趣,错误是由于在e和s指针递增之后访问它们。一个更简单的解决方案是:
For interest's sake: The faults were due to accessing the e and s pointers after they were incremented. A much simpler solution followed from that:
void something(char * str) {
char *s = new char[strlen(str) + 1];
strcpy(s, str);
char temp;
int j = strlen(str) - 1;
for (int i = 0; i <= strlen(str)/2; i++) {
cout << s << endl;
temp = s[i];
s[i] = s[j];
s[j] = temp;
j--;
}
delete [] s;
}
推荐答案
例如:
char buf[128];
const char *src = "Hello World";
strncpy(buf, src, 127); // one less - we always 0-terminate
buf[127] = '\0';
// you can now modify buf
buf[0] = 'W';
您的代码不起作用的原因是您尚未为副本分配任何内存字符串 - 你刚刚做了第二个指针到同一只读存储器。 (然后试图复制它?我不知道其余的代码是做什么)。你需要在某个地方获得一些非只读内存,使用标准库将它复制到更容易
The reason your code doesn't work is that you haven't allocated any memory for the copy of the string - you've just made a second pointer to the same read-only memory. (And then tried to copy it? I'm not quite sure what the rest of the code is doing.) You need to get some non-read-only memory somewhere, and it's much easier to use the standard library to copy it into that new memory, rather than writing the loop yourself.
如果你不知道字符串的长度,你也可以使用malloc(或者,更好的做什么drschnz的回答说,并使用 new char [] ):
In the case when you don't know the length of the string beforehand, you can also use malloc (or, even better, do what drschnz's answer says and use new char[]):
const char *src = "Hello world";
char *buf = malloc(strlen(src) + 1); // or = new char[strlen(src) + 1];
strcpy(buf, src);
// you can now modify buf
// later, you need to free it
free(buf); // or delete [] buf;
另外,如果你使用C ++,你可以使用std :: string: p>
Also, if you're using C++, you can just use a std::string:
std::string myString("Hello world");
myString[0] = "W";
希望有帮助。
这篇关于为什么不能为指针赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!