我有一个奇怪的问题,有时这个函数有时并不总是起作用,并且只有在它被称为相同的头文件中声明时才起作用...这是一件很奇怪的事情,因为编译器似乎甚至忽略了包含该函数的文件或不...

double mapRange(double value, double r1, double r2, double n1, double n2)
{
    double res = n1+(value-r1)*((n2-n1)/(r2-r1));
    printf("dividingby: %f", res);putchar('\n');
    return res;
}


如您所见,当它打印值时它是正确的,但是当我从诸如main之类的另一个函数打印返回的值时,我得到了垃圾编号...
最糟糕的是,我自己在文件上对其进行了测试,并且可以完美地工作,我知道这似乎是我的错误,但是我真的找不到对此的解释……有人遇到过类似的事情吗?我的编译器坏了吗?我不知道C中有什么限制吗?我也知道我没有比这更多的代码了,这使诊断变得很困难,但这是来自一个不太小的统一项目...

编辑1:
好吧

// main.c

 #ifndef UTIL_H
    #define UTIL_H
    #include "util.h"
    #endif /*UTIL_H*/

int main(int argc, char *argv[]) {
    Matrix* volcan = readVolcano("test.txt");
    double t;
    mapRange(7.0, 1.0, 9.0, 0.0, 1.0,&t);
    printf("%f\n..\n", t);
    mat_printinfo(volcan);
    mat_printmat(volcan, NULL);
    weighted_volcano(volcan, NULL, NULL, NULL);
    mat_free(volcan);
    return 0;
}


// util.c

#include "util.h"


/*
    Retorna una transformacion de value que esta entre r1 y r2 a un valor que esta
    entre n1 y n2, con tal que tenga la misma relacion con el rango [n1, n2] como
    value tenga con el rango [r1, r2]
*/


double mapRange(double value, double r1, double r2, double n1, double n2, double* result)
{
    double res = n1+(value-r1)*((n2-n1)/(r2-r1));
    printf("dividingby: %f", res);putchar('\n');
    if(result!=NULL)
    *result = res;
    return res;
}


我绝对确定在定义上没有重复项,我也已经多次重建了该项目...

D:\Code\C\volcan\cc1.exe    [Warning] command line option '-std=c++11' is valid for C++/ObjC++ but not for C
D:\Code\C\volcan\main.c In function 'main':
52  19  D:\Code\C\volcan\main.c [Warning] initialization makes pointer from integer without a cast
D:\Code\C\volcan\main.c In function 'open_volcano':
91  7   D:\Code\C\volcan\main.c [Warning] assignment makes pointer from integer without a cast
D:\Code\C\volcan\main.c In function 'test_volcano':
119 13  D:\Code\C\volcan\main.c [Warning] assignment makes pointer from integer without a cast
D:\Code\C\volcan\cc1.exe    [Warning] command line option '-std=c++11' is valid for C++/ObjC++ but not for C
D:\Code\C\volcan\volcantobmp.c  In function 'weighted_volcano':
38  6   D:\Code\C\volcan\volcantobmp.c  [Warning] assignment makes pointer from integer without a cast
40  6   D:\Code\C\volcan\volcantobmp.c  [Warning] assignment makes pointer from integer without a cast
D:\Code\C\volcan\cc1.exe    [Warning] command line option '-std=c++11' is valid for C++/ObjC++ but not for C


IDE的输出

编辑2:
所以看来问题出在这里

// util.h

#include "headers.h"


它是空的...

最佳答案

根据您的描述,很可能在您的“代码库”中的某个地方还有另一个“ mapRange”函数。

只是为了解决问题,我建议将您的“ mapRange”函数重命名为唯一的名称,例如追加今天的日期(mapRange_20181127),然后在main()中保留mapRange调用。如果您的“代码库”包含名称为“ mapRange”的另一个函数,则您将不会遇到任何错误,因为链接器会找到“其他” mapRange。

在这种情况下,选择另一个有意义的名称可以解决您的问题。

希望这可以帮助!

更新:
我猜我在打字的时候,您发布了更多详细信息,然后您得到了答案:)。

09-30 17:15
查看更多