So I'm trying to reverse a string, but I get a memory fault. Memory for s and s1 is initialized enough to accomodate the '/0' character as well.
所以我试图反转一个字符串,但我得到了一个内存错误。 s和s1的内存初始化足以容纳'/ 0'字符。
Interestingly if I remove *s=*s1 and print s1 instead the program works. But I haven't even set the "\0" character at the end of s1 so how does it even know where to stop printing?
有趣的是,如果我删除* s = * s1并打印s1而不是程序正常工作。但我甚至没有在s1的末尾设置“\ 0”字符,所以它甚至知道在哪里停止打印?
And in the case below what exactly is the issue?
在下面的情况下,究竟是什么问题?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(void)
{
char *s = "abcdefghijklmnop", *s1=malloc(17);
int i;
for(i=0;i<strlen(s);i++)
{
*(s1+i) = *(s+strlen(s)-1-i);
}
*s=*s1;
printf("%s",s);
}
2 个解决方案
#1
3
The char *s = "abcdefghijklmnop"
is a string literal which is often in read-only memory. An error will be generated if you attempt to modify a string literal.
char * s =“abcdefghijklmnop”是一个字符串文字,通常在只读内存中。如果您尝试修改字符串文字,将生成错误。
You attempt to replace the first character in s
with the first character in s1
when you do *s=*s+1
.
当你执行* s = * s + 1时,尝试用s1中的第一个字符替换s中的第一个字符。
If s
wasn't a string literal, you should've done s=s1
instead of *s=*s1
to make s
its reverse.
如果s不是字符串文字,你应该完成s = s1而不是* s = * s1来使s反向。
More about this:
更多关于这个:
https://softwareengineering.stackexchange.com/questions/294748/why-are-c-string-literals-read-only
String literals: Where do they go?
where in memory are string literals ? stack / heap?
https://softwareengineering.stackexchange.com/questions/294748/why-are-c-string-literals-read-only字符串文字:它们去哪儿了?内存中的字符串文字是什么?堆栈/堆?
The correct string is printed with printf("%s", s1);
even if no \0
was stored because the memory next to the last character just happened to be 0
which is equivalent to \0
. This needn't always be so and cannot be relied upon as malloc()
doesn't initialise the memory that it allocates.
用printf打印正确的字符串(“%s”,s1);即使没有存储\ 0,因为最后一个字符旁边的内存恰好是0,相当于\ 0。这并不总是如此,因为malloc()不会初始化它分配的内存,所以不能依赖它。
But calloc()
will initialise the memory it allocates to 0
.
但是calloc()会将它分配的内存初始化为0。
See more here.
在这里查看更多。
#2
0
In your code *s=*s1
only copy the first content of s1 into s. i.e, *(s+0)=*(s1+0)
not the entire string. So we need to assign the address of s1 to s. i.e, s=s1
.
在你的代码中* s = * s1只将s1的第一个内容复制到s中。即,*(s + 0)= *(s1 + 0)不是整个字符串。所以我们需要将s1的地址分配给s。即,s = s1。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *s = "abcdefghijklmnop", *s1=malloc(17);
int i;
for(i=0;i<strlen(s);i++)
{
*(s1+i) = *(s+strlen(s)-1-i);
}
s=s1;
printf("%s",s);
free(s1);
return 0;
}
It is good practice to free the memory after its use.
在使用后释放内存是一种好习惯。
#1
3
The char *s = "abcdefghijklmnop"
is a string literal which is often in read-only memory. An error will be generated if you attempt to modify a string literal.
char * s =“abcdefghijklmnop”是一个字符串文字,通常在只读内存中。如果您尝试修改字符串文字,将生成错误。
You attempt to replace the first character in s
with the first character in s1
when you do *s=*s+1
.
当你执行* s = * s + 1时,尝试用s1中的第一个字符替换s中的第一个字符。
If s
wasn't a string literal, you should've done s=s1
instead of *s=*s1
to make s
its reverse.
如果s不是字符串文字,你应该完成s = s1而不是* s = * s1来使s反向。
More about this:
更多关于这个:
https://softwareengineering.stackexchange.com/questions/294748/why-are-c-string-literals-read-only
String literals: Where do they go?
where in memory are string literals ? stack / heap?
https://softwareengineering.stackexchange.com/questions/294748/why-are-c-string-literals-read-only字符串文字:它们去哪儿了?内存中的字符串文字是什么?堆栈/堆?
The correct string is printed with printf("%s", s1);
even if no \0
was stored because the memory next to the last character just happened to be 0
which is equivalent to \0
. This needn't always be so and cannot be relied upon as malloc()
doesn't initialise the memory that it allocates.
用printf打印正确的字符串(“%s”,s1);即使没有存储\ 0,因为最后一个字符旁边的内存恰好是0,相当于\ 0。这并不总是如此,因为malloc()不会初始化它分配的内存,所以不能依赖它。
But calloc()
will initialise the memory it allocates to 0
.
但是calloc()会将它分配的内存初始化为0。
See more here.
在这里查看更多。
#2
0
In your code *s=*s1
only copy the first content of s1 into s. i.e, *(s+0)=*(s1+0)
not the entire string. So we need to assign the address of s1 to s. i.e, s=s1
.
在你的代码中* s = * s1只将s1的第一个内容复制到s中。即,*(s + 0)= *(s1 + 0)不是整个字符串。所以我们需要将s1的地址分配给s。即,s = s1。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *s = "abcdefghijklmnop", *s1=malloc(17);
int i;
for(i=0;i<strlen(s);i++)
{
*(s1+i) = *(s+strlen(s)-1-i);
}
s=s1;
printf("%s",s);
free(s1);
return 0;
}
It is good practice to free the memory after its use.
在使用后释放内存是一种好习惯。