我有以下代码片段:基本上我试图使用值集方法从提取函数中获取值。以下是
下面的代码段int extract(uint8_t *msg)
{
msg = get_value();
print(msg); // I am able to print the value here.
}
int main()
{
uint8_t msg;
extract(&msg);
print(msg) // Here it is printing incorrect value..
}
我做错了什么?
答案 0 :(得分:1)
您正在将msg作为指针传递。
int extract(uint8_t *msg)
{
msg = get_value();
print(msg); // I am able to print the value here.
}
应该是
int extract(uint8_t *msg)
{
*msg = get_value();
print(*msg);
}
答案 1 :(得分:1)
如果你把所有给出的答案结合起来,你可能会得到一些有用的东西。
您的代码中存在许多错误,并且不清楚get_value()
返回的内容以及print()
所需的参数是什么。
我的猜测是你的print()
函数需要一个指针 - 这就是print()
在extract()
中工作的原因 - 你也应该通过引用传递msg指针使它在main中工作。所以你可能需要这样的东西(C代码):
int extract(uint8_t **msg)
{
(*msg) = get_value();
print(*msg);
}
int main()
{
uint8_t *msg;
extract(&msg);
print(msg)
}
答案 2 :(得分:0)
msg = get_value()为指针赋值。您应该使用* msg = get_value()将值赋给main中定义的变量msg。