【问题标题】:How can I use the result from a function as a parameter to another function in C?如何将函数的结果用作 C 中另一个函数的参数?
【发布时间】:2020-03-14 02:29:51
【问题描述】:

在返回 void 且没有参数的函数中,我想使用另外两个函数(不幸的是,我无法修改它)。这些函数中的第一个计算 int 的结果,我想将该结果作为参数传递给第二个函数。我怎么能做到这一点?谢谢。

void descriptionEdit(void)
{
   while(1)
   {
      int fieldno;
      if (!pluFields())
      {
        break;
      }
      start_editor(setf, esize);
      do
      {
         //code
      } while(fieldno>=0);
      saveEditor();
   }
}


bool pluFields(void)
{
   **edplu** = search_productcode();
   if (edplu == 0)
   {
      return true;
   }
}

void saveEditor()
{
   if (save_plu(**edplu**, &plu))
   {
      CloseProductIniFile();
      cls();
      writeat(30, 6, INV2X3, "Save Confirmed");
   }
}

所以,我想以某种方式使用 pluFields 函数中的 edplu 作为 saveEditor 函数的参数,我想将 edplu 设为全局变量,但我认为这是一个不好的做法

【问题讨论】:

  • 我猜这是伪代码,但请注意并非所有路径都返回pluFields中的值
  • 也不确定您发布的代码如何与您的问题相关。我没有看到返回 int 的函数或接受参数的函数。
  • @yano 是的,它的伪代码。为误解道歉,我想说的是一个计算 int 而不是返回 int 的函数,所以我忘了提到 edplu 是一个 int 变量。至于接受参数的函数是 saveEditor 函数,所以我想在该函数中传递 edplu,这就是为什么我只留下括号
  • 哦,好吧..很高兴你被摆平了。

标签: c function parameters


【解决方案1】:

您的算法非常不直观。但是,要获取函数的值并传递给另一个函数,请执行以下操作:

int functionName(int param1) { 
  // calculate values
  return variable_here
}

int value = functionName(param1)

然后 value 将进入下一个函数的参数,就像那样

void function2(int value) ...
function2(value)

如果我有帮助,请告诉我。

【讨论】:

  • 这就是我要找的,非常感谢!
猜你喜欢
  • 2018-06-30
  • 2022-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多