为什么这个字符串反转 C 代码会导致分段错误? [英] Why is this string reversal C code causing a segmentation fault?

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

问题描述

我正在尝试编写代码来原地反转字符串(我只是想在 C 编程和指针操作方面做得更好),但我不明白为什么我会收到 分段错误:

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:

#include <string.h>

void reverse(char *s);

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

    return 0;
}

void 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 行.我知道可能有更好的方法来做到这一点,但我有兴趣找出在我的代码中具体是什么导致了分段错误.

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.

推荐答案

仅凭那段代码无法说明.最有可能的是,您传入的指针指向无效内存、不可修改的内存或某些其他类型的内存,这些内存无法按照您在此处的处理方式进行处理.

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";

指针直接指向字符串字面量的开头.在一般情况下,任何修改 s 所指内容的尝试都被视为失败.你可以读它,但你不能写进去.出于这个原因,强烈建议仅使用指向常量的指针指向字符串文字

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";

但是当您将 s 声明为

char s[] = "teststring";

你得到一个完全独立的数组 s 位于普通的可修改内存中,它只是用字符串文字初始化.这意味着独立的可修改数组 s 将从字符串文字中获得它的初始值 复制.之后,您的 s 数组和字符串文字继续作为完全独立的对象存在.文字仍然是不可修改的,而您的 s 数组是可修改的.

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 代码会导致分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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