在函数中更改指针的值

编程入门 行业动态 更新时间:2024-10-26 18:23:26
本文介绍了在函数中更改指针的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在函数中更改变量的值. 我的代码是这样的:

I want to change the variable's value in the function. my code is like this:

void change(char *buf){ char str = "xxxxxxx"; *buf = &str; } int main(){ char *xxx = NULL; change(xxx); }

当我用valgrind调试时,它说:

when I debug with valgrind, it says:

==3709== Invalid write of size 1 ==3709== at 0x80483CA: change (test.c:5) ==3709== by 0x80483E5: main (test.c:10) ==3709== Address 0x0 is not stack'd, malloc'd or (recently) free'd ==3709== ==3709== ==3709== Process terminating with default action of signal 11 (SIGSEGV) ==3709== Access not within mapped region at address 0x0 ==3709== at 0x80483CA: change (test.c:5) ==3709== by 0x80483E5: main (test.c:10)

有人可以帮助我吗?我是C的新手.

Can anyone help me? I'm new in C....

推荐答案

使用指向指针的指针:

void change(char **buf) { *buf = "xxxxxxx"; } int main(void) { char *xxx = NULL; change(&xxx); }

更多推荐

在函数中更改指针的值

本文发布于:2023-10-25 22:27:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528292.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!