【问题标题】:Passing a pointer of a structure from a function to another function将结构的指针从函数传递到另一个函数
【发布时间】:2014-10-06 14:02:27
【问题描述】:

我有一个全局结构。我正在使用函数来更改结构的数据。 一般来说,我很容易操纵结构(例如,将其发送到函数)。 我的问题是现在我有一个改变结构数据的函数,但它还必须调用另一个函数。这是我的问题。

一般来说,我的结构是:“名称”。 我有一个指针:name_ptr->name[i].... 如果是函数,我将像这样传递它: find_max = calc_max(i, &name_ptr); 并且此函数 id 声明为: int find_max(int x, vectname **pr) 所以在函数内部我是这样工作的: (*pr)->name[i]... 如果我进入函数并且想将此指针发送到另一个函数,我该如何调用它?

【问题讨论】:

  • 简单调用nextFunction(pr)。定义应该是nextFunction(vectname **arg){}
  • “结构的结构”是值得展示的东西。请尝试并记住您可以看到 all 您现有的代码;我们可以准确地看到它没有。将其剥离到您在选择正确语法时遇到问题的特定点,然后发布。照原样需要一个镐式夜视镜和一个指南针来解决这个问题。

标签: c function structure


【解决方案1】:

简单来说,你有一个结构数组,你的第一个函数得到指向数组的指针。

所以你可以使用:

int find_max(int x, vectname **pr)
{

  //  method - 1 (if your second function is intreseted only in changing the nth structure instance of the array )
   SecondFunc( &pr[n]);

  //  method - 2 (if your second function is intreseted in changing the any structure instance of the array )
   SecondFunc( pr );

}

【讨论】:

    【解决方案2】:

    你的问题很神秘,但我会试一试:

    int find_max(int x, vectname **pr)
    {
      ...
      another_function(x, pr);
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 2015-05-09
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 2021-11-02
      相关资源
      最近更新 更多