【问题标题】:Passing a variable function v->x to the int main function()将变量函数 v->x 传递给 int main function()
【发布时间】:2016-03-10 16:14:21
【问题描述】:

我想将我的 v->x 从我的函数“calculations_using_struct_values”传递给我的“int main”函数。为了让我的“int main”函数和“int main”原型运行程序,我必须在括号内写什么。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void calculations_using_struct_values(struct test *v);
int main();

struct test
{
    int x,y,z;
    char *name;
};

void struct_values()
{
    test variable;

    variable.x=50;
    variable.y=100;
    variable.z=150;

    calculations_using_struct_values(&variable);
    return;
}

void calculations_using_struct_values(struct test *v)
{
    int a=5;
    v->x +=a;

    main();
}

int main()
{
    return 0;
}

【问题讨论】:

  • 假设一个托管环境:您不应为main 提供声明(您会这样做 - 您的编译器是否已经发出警告?)。 OTOH,main 在没有原型的情况下调用它时未定义。仅此一项就可以强烈暗示从代码中的另一个函数调用main 是一个非常糟糕的主意。事实上,这被认为是非常糟糕的做法 - 只是不要!并且不要使用旧式声明符,而是正确的原型。

标签: c parameter-passing pass-by-reference


【解决方案1】:

代替

void calculations_using_struct_values(struct test *v)
{
    int a=5;
    v->x +=a;

    main(); /* remove this call  */
}

做:

int main(void)
{
    struct_values(); /* call your function in main */
    return 0;
}

【讨论】:

  • 别担心,我发现了。我想在 void struct_values 中调用 struct 测试变量。总之谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多