交换C ++数组的2个字符(Swap 2 char of a C++ array)
我有内存访问问题。 当i = 0 ,Visual Studio会引用一个异常并引用一行,如下面的代码所示。
无法访问0x00AD8B3B和0x00AD8B3B等于scr+np-i
如何修复for循环体以解决此问题?
int o_strrev(char* scr)
{
int np = strlen(scr) - 1;
char tmp;
if (!scr) return -1;
if (!*scr) return -1;
for (int i = 0; i < np / 2; i++)
{
tmp = scr[np-i];
scr[np-i] = scr[i]; # this line
scr[i] = tmp;
}
return 0;
}
I have a problem with memory access. When i = 0, Visual Studio throws an exception with reference to a line as labelled in the following code.
Can't access at 0x00AD8B3B and 0x00AD8B3B equals scr+np-i
How can I fix the for-loop body so as to fix this issue?
int o_strrev(char* scr)
{
int np = strlen(scr) - 1;
char tmp;
if (!scr) return -1;
if (!*scr) return -1;
for (int i = 0; i < np / 2; i++)
{
tmp = scr[np-i];
scr[np-i] = scr[i]; # this line
scr[i] = tmp;
}
return 0;
}
原文:https://stackoverflow.com/questions/34872307
更新时间:2020-02-26 12:47
最满意答案
正如@Revolver_Ocelot所指出的,你可能正在从字符串文字中传递一个const char* 。 因为根据定义,这些是不变的,所以你不能按照你想要的方式修改它们。 您需要一些方法将const char*转换为非常量char* 。 这些方面的东西对你有用:
string str = "string";
char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
我使用非常量char*以g ++编译你的代码,它工作正常。 只要记住在你完成它之后解除你的char* 。 我们不希望内存泄漏;)
As pointed out by @Revolver_Ocelot, you are probably passing a const char* from a string literal. Since these are, by definition, constant, you can't modify them in the way you're trying to. You need some way to convert const char* into a non constant char*. Something along these lines will work for you:
string str = "string";
char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
I've compiled your code in g++ using non constant char* and it works fine. Just remember to deallocate your char* when you're done with it. We don't want memory leaks ;)
2016-01-19
相关问答
你不能修改字符串文字。 而是试试这个: char s[] = "ABC"
swap(s, 0, 1);
printf("%s\n", s);
You can't modify a string literal. instead try this: char s[] = "ABC"
swap(s, 0, 1);
printf("%s\n", s);
在if和else if你需要使用==而不是= 。 在C ++ / C中,您使用==进行比较,使用=进行赋值。 In if and else if you need to use == instead of =. In C++/C you use == for comparison and = for assignment.
这是典型的缓冲区溢出。 这就是为什么你总是应该检查输入的大小,如果你把它放在缓冲区。 这是发生了什么: 在C ++(和C)中,数组名只是指向数组的第一个元素的指针。 编译器知道数组的大小,并会执行一些编译时检查。 但是,在运行时,它会将其视为char *。 当你完成cin >> name1 ,你将char *传递给了cin 。 cin不知道分配的空间有多大 - 它只有一个指向某个内存的指针。 因此,它假定您分配了足够的空间,写入所有内容,并且超过了数组的末尾。 这是一张图片: Bytes 1
...
你需要检查str[i] 不等于 '\n'而不是检查i!='\n' 。 正如@BLUEPIXY指出的那样,它意味着i!= 10,在ASCII代码中'\ n'等于10。 所以改变条件为: for (int i=0;i
You need to check that str[i] is NOT EQUAL to '\n' instead of checking i!='\n'. As @BLUEPIXY pointed out it m
...
至少你遇到错误的一个问题是你正在改变参数p1的地址,而你不存储你附加A的原因。 你没有将所有的字符添加到s字符串中 希望代码吼叫有帮助。 void StrAdd_A_afterEach_CD(char *param)
{
char* p1 = param;
char s[100] = {0};
char *p2 = s;
while( *p1 )
{
bool processed = false;
if(*p1 == 'c
...
因为你的字符串复制代码是错误的。 它也会复制字符串末尾的空字节。 尝试这个 while (*a) // Copy a into buffer
*p++ = *a++;
while (*b) // Copy b into buffer
*p++ = *b++;
等等 Because your string copying code is wrong. It copies the null bytes at the end of the strings too. Try this w
...
正如@Revolver_Ocelot所指出的,你可能正在从字符串文字中传递一个const char* 。 因为根据定义,这些是不变的,所以你不能按照你想要的方式修改它们。 您需要一些方法将const char*转换为非常量char* 。 这些方面的东西对你有用: string str = "string";
char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
我使用非常量char*以g ++编译你的代码,它工作正
...
您需要获取std::string s中的数据地址。 注意,这些不需要以空值终止,即,您需要确保所有字符串都以空值终止。 另外,作为argv传递的数组也需要将最后一个元素作为空指针。 您可以使用以下代码: std::string array[] = { "s1", "s2" };
std::vector vec;
std::transform(std::begin(array), std::end(array),
std::back_inserter(ve
...