有一个函数改变数值的指针再presents用C [英] Having a function change the value a pointer represents in C

查看:121
本文介绍了有一个函数改变数值的指针再presents用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一个char一个功能,我试图传递一个指向字符进入一个功能,有它从 A 更改为 B ,但它只是似乎并没有改变它。这里显示的例子仅仅是code的当前状态,我已经尝试了它许多不同的变化从而有可能在没有其他错误,从简单的救命稻草抓着。

  INT的main()
{
    焦炭结果='A';
    setChar(安培;结果);
    的printf(%C,结果);
}无效setChar(字符* charToChange)
{
    charToChange =B;
}


解决方案

您需要的是 * charToChange ='B'; 。指针 charToChange setChar 局部变量(参数),但你可以改变它指向使用preFIX * 运营商和分配。需要注意的是 * charToChange 是一个字符,也没有一个字符串。

I have a main function that has a char, I am attempting to pass a pointer to that char into a function and have it change it from A to B but it just doesn't seem to change it. The example shown here is just the current state of the code I have tried many different variations on it thus there may be other mistakes in there from simply clutching at straws.

int main()
{
    char result = 'A';
    setChar(&result);
    printf("%C", result);
}

void setChar(char* charToChange)
{
    charToChange = "B";
}

解决方案

What you want is *charToChange = 'b';. The pointer charToChange is a local variable (parameter) in setChar, but you can change what it points to using the prefix * operator and an assignment. Note that *charToChange is a character, too, not a string.

这篇关于有一个函数改变数值的指针再presents用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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