C float在传递给函数后重置为0

标签 c function pointers

在下面的代码中,变量“tmpRes”在调用“BuildCMD”之前是正确的 但是在这个函数内部,它失去了他的正确值并设置为 0,为什么它甚至在 BuildCMD 函数内部也没有保持正确的值?

调用代码:

//tmpRead just an array of integer
float tmpRes=0;
Evaluate(tmpRead[3],tmpRead[4],tmpRead[5],&tmpRes);
printf("PRE : %f\n",tmpRes);   //correct result
char *dataBuff=BuildCMD(RES,tmpData,tmpRes);

评估代码:

int Evaluate(int num1,int op,int num2,float *Res)
{
     float tmpRes=0;
     switch(op)
     {
           case(int)'+':{tmpRes=num1+num2;break;} //same with *Res=....
           case(int)'-':{tmpRes=num1-num2;break;}
           //etc...
     }
    *Res=tmpRes;
    return 0;

构建命令:

char* BuildCMD(enum CMD cmd,int *values,float result)
{
     //here the result is ALWAYS 0
     //even if it was corrent before the call of BuildCMD
     printf("IN: %f\n,result);   
     fflush(stdout);
     //...rest of the code
}

提前致谢。

是的,应用程序是多线程的,我从服务器创建了 mainClient 的 n 个实例。 链接到源代码(我上面写的那个是简化的)。 mainClient.c Utilities.c

最佳答案

听起来调用 BuildCMD 的代码没有适合该函数的原型(prototype)。

您需要在 main.c 或 main.c 包含的 .h 文件中使用此原型(prototype):

char* BuildCMD(enum CMD cmd,int *values,float result);

关于C float在传递给函数后重置为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17040532/

相关文章:

python - 使用 function 或 var 创建数据框列/索引名称

c - 将 char 分配给 char 指针时出现段错误(核心转储)

c - 结构体成员的指针

我可以将 __func__ 替换为 C 宏中的标识符名称吗?

c - 如何参数化提交到外部库的回调函数

c - 我如何可视化后递增指针的概念?

c++ - 为什么属性不能在密封的引用类中公开

c - 关于 sigprocmask()、SIG_BLOCK 和 SIG_SETMASK

php - 获取两个mysql选择之间的值差异

C函数的隐式声明