重新分配指针的值 [英] reassigning value of a pointer

查看:64
本文介绍了重新分配指针的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在下面的代码中我试图更改字符串的内容:


int main()

{

char * string =" testing" ;;

rename(string);


返回0;

}


无效重命名(字符* s)

{

printf( 字符1:%c \ n,* s);

* s =''a'';

printf("字符1现在:% c \ n,* c);

}


无论如何,我确信这是可能的但是出于某些原因我得到了/>
分段错误在行* s =''a'';当我试图更改当前指向* s的值

时,是

字符串中的第一个字符。


任何想法?


提前致谢,


Ben。

Hi all,

In the following code I am trying to change the contents of a string:

int main()
{
char *string="testing";
rename(string);

return 0;
}

void rename(char *s)
{
printf("Character 1: %c\n",*s);
*s=''a'';
printf("Character 1 now: %c\n",*c);
}

Anyway, I am sure this is possible but for some reason I''m getting
segmentation faults at line *s=''a''; when I attempt to change the value
in which *s is currently pointing to, being the first character in the
string.

Any ideas?

Thanks in advance,

Ben.

推荐答案

在文章< 11 ********************** @ g14g2000cwa.googlegroups .com> ;,

< bh ****** @ dodo.com.au>写道:
In article <11**********************@g14g2000cwa.googlegroups .com>,
<bh******@dodo.com.au> wrote:
在下面的代码中,我试图更改字符串的内容:
int main()
{char / string =" testing" ;
In the following code I am trying to change the contents of a string: int main()
{
char *string="testing";




该标准允许字符串文字存储在只读内存中。

将字符串文字赋值给指针设置指针值

到{可能只读}内存的地址。


特别是char * string =" testing" ;;通常不会在某处分配一些

内存,并在运行时将字符串复制到其中。这是

可能的行为之一,但它不是唯一可能的行为。


另一个需要注意的是,允许编译器

合并所有字符串文字 - 例如,如果你还有


char * anotherstring =" ;测试" ;;


然后anotherstring最终可能与你的字符串变量相同的指针值

。此外,如果你有
char * thirdstring ="只是测试" ;;


那么string和anotherstring可能最终指向终端

测试 只是测试的子串。


如果你需要一个可修改的字符串,分配内存(不知何故)和

将相应的内容复制到其中。

-

重要的是要记住,在法律方面,电脑

永远不会制作副本,只有人类制作副本。计算机给出了

命令,而非许可。只有人才能获得许可。

- Brad Templeton



The standard allows string literals to be stored in read-only memory.
An assignment of a string literal to a pointer sets the pointer value
to the address of that {possibly read-only} memory.

In particular, char *string="testing"; usually does not allocate some
memory somewhere and copy the string into it at runtime. That''s
one of the possible behaviours, but it isn''t the only possible
behaviour.

Another thing to note is that it is allowed for the compiler
to merge all string literals -- so for example if you also had

char *anotherstring="testing";

then anotherstring could end up as the same pointer value as
your string variable. Furthermore, if you had

char *thirdstring="just testing";

then string and anotherstring could end up pointing at the terminal
"testing" substring of the "just testing".

If you need a modifiable string, allocate the memory (somehow) and
copy the appropriate contents into it.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton


感谢Walter提供的信息。我设法让它工作,通过

分配内存,然后使用strcpy加载字符串。


问候,


Ben。

Thanks for that information Walter. I managed to get it working, by
allocating the memory then using strcpy to load the string.

Regards,

Ben.


bh *** ***@dodo.com.au 写道:
大家好,

在下面的代码中,我试图更改字符串的内容:


然后不要使用字符串文字。

int main()
{
char * string =" testing" ;;
Hi all,

In the following code I am trying to change the contents of a string:
Then don''t use string literals.

int main()
{
char *string="testing";




如果你想改变字符串,不要让''string''指向文字

常数;使用数组:

char string [] =" testing";


发布前请务必查看常见问题解答。在这种情况下,您可能已经找到了

< http://www.eskimo.com/~scs/C-faq/q1.32.html>有用。



If you want to change the string, don''t make ''string'' point to a literal
constant; use an array:
char string[] = "testing";

Always check the FAQ before posting. In this case, you might have found
<http://www.eskimo.com/~scs/C-faq/q1.32.html> useful.


这篇关于重新分配指针的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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