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

查看:115
本文介绍了是否有可能修改的char在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数组工作的罚款)。

Thanks everyone for your answers. It makes more sense now. It especially makes sense why sometimes it was working fine and other times not working. Because sometimes I'd pass a char pointer and other times a char array (the char array worked fine).

推荐答案

当你在你的源代码code写了一个串,它就会被直接应用,因为该值需要在编译时被称为写入可执行文件(有可拉开软件和找到所有的明文串在其中)的工具。当你写的char * 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()记得打电话空间免费()当你与它完成(读:内存泄漏)

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

基本上,你要跟踪的数据所在的位置。每当你在你的源代码写一个字符串,该字符串只读(否则你将可能改变可执行文件的行为 - 想象一下,如果你写的char * A =你好; 再变 A [0] 'C',然后在其他地方写了的printf(你好); 如果你被允许改变的第一个字符你好,和你的编译器只存储一次。 (应该),那么的printf(你好!); 将输出大提琴

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!)

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

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