C 阿托夫创造了奇怪的结果

C 阿托夫创造了奇怪的结果,c,debugging,atof,C,Debugging,Atof,我试图使用atof()将字符串转换为double(显然),但结果并不是我所期望的。以下是有关atof()之前的变量值的代码和调试信息: 程序通过atof()后。结果如下: arg = 0x0034f7b0 "ôþSÄ÷4" d = 0.0000000000000000 如您所见,在命令之前,arg变量确实包含有效的double。但是,返回值为0。让我困惑的是,为什么arg的值会改变 另外,我也包含了stdlib.h。此外,arg声明为: char *arg; 如果有帮助的话,“10.0”是从

我试图使用atof()将字符串转换为double(显然),但结果并不是我所期望的。以下是有关atof()之前的变量值的代码和调试信息:

程序通过atof()后。结果如下:

arg = 0x0034f7b0 "ôþSÄ÷4"
d = 0.0000000000000000
如您所见,在命令之前,arg变量确实包含有效的double。但是,返回值为0。让我困惑的是,为什么arg的值会改变

另外,我也包含了stdlib.h。此外,arg声明为:

char *arg;
如果有帮助的话,“10.0”是从文件中读取的

更多代码:

void read_instructions()
{
    char *str;
    char *arg;
    int n;
    char c;
    double d = 0;
    instruction next = {0};

    while (!feof(datafile)) {
        // Fetch the next string
        // if (push or pop), get the next argument
        // create instructiwn and add to instruction array
        str = get_next_string();

        if (strncmp (str, "P", 1) == 0) {
            if (strncmp (str, "PUSH", 4) == 0) {
                next.func = pushFunc;
            }
            else {
                next.func = popFunc;
            }
            arg = get_next_string();
            n = arg[0];
            if (n > 64 && n < 71)
                next.ch = arg[0];
            else {
                d = atof (arg);
                next.db = d;
            }
            instr[instr_count] = next;
            instr_count++;
        }
    else {
        c = str[0];


        switch (c) {
        case 'A' :
            next.func = addFunc;
            break;
        case 'S' :
            next.func = subFunc;
            break;
        case 'M' :
            next.func = multFunc;
            break;
        case 'D' :
            next.func = divFunc;
            break;
        case 'H' :
            next.func = haltFunc;
        default :
            printf ("Invalid instruction");
        }
        instr[instr_count] = next;
        instr_count++;
    }
}
fclose (datafile);
}
头文件:

#ifndef MAIN_HEADER_H
#define MAIN_HEADER_H

#define INSTR_SIZE 30

typedef struct {
    void (*func)(instruction);
    union {
        double db;
        char ch;
    };
} instruction;

int main(int, char*);
char* get_next_string();
void open_file(char*);
void read_instructions();
void execute_instructions();
void pushFunc(instruction instr);
void popFunc(instruction instr);
void addFunc(instruction instr);
void subFunc(instruction instr);
void multFunc(instruction instr);
void divFunc(instruction instr);
void haltFunc(instruction instr);

#endif
这是测试文件:

PUSH 10.0
PUSH 4.0
PUSH 7.0
PUSH 5.0
POP D
POP E
POP 
PUSH D
ADD
PUSH 5.0
POP B
PUSH 17.0
POP E
PUSH B
PUSH E
SUB
HALT

您的问题可能是由
get\u next\u string()
函数返回指向临时本地字符数组的指针造成的。一旦函数返回,由
str[]
使用的堆栈内存将被其他自动变量覆盖。这可以解释为什么
arg
被损坏

有几种可能的修复方法

  • 调用者可以分配内存来保存字符串,并将此指针传递给函数,以便它填充数据
  • 被调用方可以为字符串分配内存并返回该指针。这还将所有权返还给调用者,并在完成后负责调用
    free()
  • 可以在函数内部声明
    str[]
    数组
    static
    。它不再是临时的,但要注意,每次调用函数时,前面的字符串都会被覆盖

您能显示整个代码吗?很可能你在那里覆盖了什么。覆盖了什么?唯一涉及的是变量和函数调用。我不确定。您显示的代码似乎没有任何问题。给我们一些代码,我们可以复制的效果。请展示一个小完整的程序,展示问题。对你有用吗?文件的编码是什么?这很有意义。你有什么建议吗?@Troncoso:我在回答中添加了一些建议。
#ifndef MAIN_HEADER_H
#define MAIN_HEADER_H

#define INSTR_SIZE 30

typedef struct {
    void (*func)(instruction);
    union {
        double db;
        char ch;
    };
} instruction;

int main(int, char*);
char* get_next_string();
void open_file(char*);
void read_instructions();
void execute_instructions();
void pushFunc(instruction instr);
void popFunc(instruction instr);
void addFunc(instruction instr);
void subFunc(instruction instr);
void multFunc(instruction instr);
void divFunc(instruction instr);
void haltFunc(instruction instr);

#endif
PUSH 10.0
PUSH 4.0
PUSH 7.0
PUSH 5.0
POP D
POP E
POP 
PUSH D
ADD
PUSH 5.0
POP B
PUSH 17.0
POP E
PUSH B
PUSH E
SUB
HALT