是否可以在 C 中修改一串字符? [英] Is it possible to modify a string of char in C?

查看:16
本文介绍了是否可以在 C 中修改一串字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为各种与指针相关的 C 教程和书籍苦苦挣扎了几个小时,但我真正想知道的是,一旦创建了字符指针,是否可以更改它.

I have been struggling for a few hours with all sorts of C tutorials and books related to pointers but what I really want to know is if it's possible to change a char pointer once it's been created.

这是我尝试过的:

char *a = "This is a string";
char *b = "new string";

a[2] = b[1]; // Causes a segment fault

*b[2] = b[1]; // This almost seems like it would work but the compiler throws an error.

那么有没有办法改变字符串内部的值而不是指针地址?

So is there any way to change the values inside the strings rather than the pointer addresses?

推荐答案

当您在源代码中编写字符串"时,它会直接写入可执行文件中,因为需要在编译时知道该值(有工具可用于将软件分开并找到其中的所有纯文本字符串).当你写char *a = "This is a string"时,"This is a string"的位置在可执行文件中,a指向的位置,是在可执行文件中.可执行映像中的数据是只读的.

When you write a "string" in your source code, it gets written directly into the executable because that value needs to be known at compile time (there are tools available to pull software apart and find all the plain text strings in them). When you write char *a = "This is a string", the location of "This is a string" is in the executable, and the location a points to, is in the executable. The data in the executable image is read-only.

您需要做的(正如其他答案所指出的)是在非只读位置创建该内存 - 在堆上或在堆栈帧中.如果您声明一个本地数组,那么该数组的每个元素都会在堆栈上留出空间,并将字符串文字(存储在可执行文件中)复制到堆栈中的该空间.

What you need to do (as the other answers have pointed out) is create that memory in a location that is not read only--on the heap, or in the stack frame. If you declare a local array, then space is made on the stack for each element of that array, and the string literal (which is stored in the executable) is copied to that space in the stack.

char a[] = "This is a string";

您还可以通过在堆上分配一些内存来手动复制该数据,然后使用 strcpy() 将字符串文字复制到该空间中.

you can also copy that data manually by allocating some memory on the heap, and then using strcpy() to copy a string literal into that space.

char *a = malloc(256);
strcpy(a, "This is a string");

每当您使用 malloc() 分配空间时,请记住在完成后调用 free()(阅读:内存泄漏).

Whenever you allocate space using malloc() remember to call free() when you are finished with it (read: memory leak).

基本上,您必须跟踪数据的位置.每当您在源代码中写入字符串时,该字符串都是只读的(否则您可能会更改可执行文件的行为——想象一下,如果您编写了 char *a = "hello"; 然后更改a[0]'c'.然后在别处写了 printf("hello");.如果你被允许改变第一个"hello" 的字符,并且你的编译器只存储它一次(它应该),然后 printf("hello"); 将输出 cello!)

Basically, you have to keep track of where your data is. Whenever you write a string in your source, that string is read only (otherwise you would be potentially changing the behavior of the executable--imagine if you wrote char *a = "hello"; and then changed a[0] to 'c'. Then somewhere else wrote printf("hello");. If you were allowed to change the first character of "hello", and your compiler only stored it once (it should), then printf("hello"); would output cello!)

这篇关于是否可以在 C 中修改一串字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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