gpt4 book ai didi

c - 如何在程序中将变量传递给稍后/将 double 输入对话框

转载 作者:太空宇宙 更新时间:2023-11-04 04:56:54 25 4
gpt4 key购买 nike

我试图在我的程序开始时创建一个对话框,允许用户输入一个数字,然后在程序稍后的另一个函数(实际上是另一个 C 文件)中使用该数字。

void function()
{
double variable;
char buf[256] = "400";
sprintf( buf, "%d", variable);
#ifdef WIN32
edit_dialog(NULL,"Enter variable", "Please enter the variable:", buf,260);
#endif
variable = atof(buf);
}

我想稍后在程序中将“变量”传递给另一个函数。问题是,直到很久以后我才需要这个变量。在它到达程序的正确部分之前,我不想在每个函数之间传递它。我该怎么做?

此外,当我启动它时,我得到了我预期的对话框,但框中的数字不是我预期的 400。相反,它是 2089881670(!) 我假设我处理内存不正确但不明白为什么。

最佳答案

对于 2089881670 问题,您应该像这样初始化您的变量:

双变量=0;

对于在某处创建并在很远的地方使用的变量,您可以使用全局/静态变量(原文如此),如下所示:

static double variable;

void function()
{
char buf[256] = "400";
sprintf( buf, "%d", variable);
#ifdef WIN32
edit_dialog(NULL,"Enter variable", "Please enter the variable:", buf,260);
#endif
variable = atof(buf);
}

关于c - 如何在程序中将变量传递给稍后/将 double 输入对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6031379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com