为什么这个字符串逆转C $ C $ç造成分段错误? [英] Why is this string reversal C code causing a segmentation fault?

查看:84
本文介绍了为什么这个字符串逆转C $ C $ç造成分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写code扭转到位的字符串(我只是想获得在C编程和指针操作更好),但我不明白,为什么我得到一个的segmentation故障

I am trying to write code to reverse a string in place (I'm just trying to get better at C programming and pointer manipulation), but I cannot figure out why I am getting a segmentation fault:

int main() {
    char* s = "teststring";
    reverse(s);

    return 0;
}

reverse(char* s) {
    int i,j;
    char temp;

    for (i=0,j=(strlen(s)-1); i<j; i++,j--) {
        temp = *(s+i);     //line 1
        *(s+i) = *(s+j);   //line 2
        *(s+j) = temp;     //line 3
    }
}

这是第2行和3所造成分段错误。据我所知,可能有更好的方法来做到这一点,但我感兴趣的是找出什么特别的在我的code 的原因造成的分段错误。

It's lines 2 and 3 that are causing the segmentation fault. I understand that there may be better ways to do this, but I am interested in finding out what specifically in my code is causing the segmentation fault.

更新:我已经包含的要求调用函数

Update: I have included the calling function as requested.

推荐答案

有没有办法从只是code说。最有可能的,你是传递一个指向无效的内存,不可修改的存储器或一些其他类型的内存,只是不能处理你在这里处理它的方式指针。

There's no way to say from just that code. Most likely, you are passing in a pointer that points to invalid memory, non-modifiable memory or some other kind of memory that just can't be processed the way you process it here.

你怎么调用你的函数?

补充:你是传递一个指向一个字符串。字符串文字是不可修改的。你不能扭转一个字符串。

Added: You are passing in a pointer to a string literal. String literals are non-modifiable. You can't reverse a string literal.

传递给修改的字符串,而不是

Pass in a pointer to a modifiable string instead

char s[] = "teststring";
reverse(s);

这已经在这里已经解释死亡。 的TestString是一个字符串文字。字符串文字本身是不可修改的对象。在实践中可能的编译器(和会)把它放在只读存储器。当你初始化一个这样的指针

This has been explained to death here already. "teststring" is a string literal. The string literal itself is a non-modifiable object. In practice compilers might (and will) put it in read-only memory. When you initialize a pointer like that

char *s = "teststring";

直接在字符串的开始指针指向。任何试图修改什么取值指向被认为在一般的情况下失败。可以读取它,但你不能写进去。由于这个原因,强烈建议指向字符串的指针与给const变量仅

the pointer points directly at the beginning of the string literal. Any attempts to modify what s is pointing to are deemed to fail in general case. You can read it, but you can't write into it. For this reason it is highly recommended to point to string literals with pointer-to-const variables only

const char *s = "teststring";

但是,当你宣布你的取值

char s[] = "teststring";

你会得到一个完全独立的阵列取值位于普通修改内存中,这仅仅是初始化的使用字符串。这意味着,独立的修改阵列取值将获得其初始值的复制的从字符串。之后,你的取值数组和字符串继续存在,作为完全独立的对象。字面仍然是不可修改的,而你的取值数组是可以修改的。

you get a completely independent array s located in ordinary modifiable memory, which is just initialized with string literal. This means that that independent modifiable array s will get its initial value copied from the string literal. After that your s array and the string literal continue to exist as completely independent objects. The literal is still non-modifiable, while your s array is modifiable.

基本上,后者声明是功能等效

Basically, the latter declaration is functionally equivalent to

char s[11];
strcpy(s, "teststring");

这篇关于为什么这个字符串逆转C $ C $ç造成分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆