如何将函数作为C中另一个函数的参数传递?

时间:2022-05-30 21:44:44

I realize that there are already too many questions about my question but the answers I have seen so far assumes the return values of the function that is to be a pointer, int value.

我意识到关于我的问题已经有太多问题,但到目前为止我看到的答案假设函数的返回值是指针,int值。

I have two functions, namely void binarycheck(int encoding, product *curr[], int totalcurr) and void search_show().

我有两个函数,即void binarycheck(int encoding,product * curr [],int totalcurr)和void search_show()。

binarycheck basically gets the encoding value from the array struct named curr (curr contains mixed variables of strings and int). Its purpose is to classify the array curr into 3 major types and 5 subclasses after checking the encoding of each array curr via bitwise function.

binarycheck基本上从名为curr的数组结构中获取编码值(curr包含字符串和int的混合变量)。其目的是在通过按位函数检查每个阵列curr的编码后,将数组curr分为3个主要类型和5个子类。

The subclasses are included within the typedef struct that was used to form curr. Like:

子类包含在用于形成curr的typedef结构中。喜欢:

typedef struct nodebase{
    char name[254];
    char company_name[254];
    int encoding;
    double price;
    struct nodebase *next;
    struct nodebase *Class;
}product;

Now, after using the binarycheck function, void search_show will basically ask user for his preferred 1) Type then 2) Subclass then void search_show will print the products that has been classified as the user's chosen Type and Subclass.

现在,在使用binarycheck函数之后,void search_show将基本上询问用户他的首选1)类型然后2)Subclass然后void search_show将打印已被分类为用户选择的Type和Subclass的产品。

So, void search_show will have to use the results of void binarycheck that are in forms of int (for counting the presence of each classified arrays) and arrays (for classifying data).

因此,void search_show必须使用void binarycheck的结果,这些结果以int的形式(用于计算每个分类数组的存在)和数组(用于分类数据)。

I was thinking of a "brute force" way by putting all variables used by void binarycheck as the parameter of void search_show but the variables are too much to add.

我想通过将void binarycheck使用的所有变量作为void search_show的参数来考虑“强力”方式,但变量太多而无法添加。

Can anyone help me how to "re-use" all the variables and the results of the first function in the second function?

任何人都可以帮我如何“重用”所有变量和第二个函数中第一个函数的结果?

*I am using Tiny C on Windows

*我在Windows上使用Tiny C.

Sample Code: //Binarycheck will contain variables shown below:

示例代码:// Binarycheck将包含如下所示的变量:

void *binarycheck(int encoding, hero *curr[], int totalwarrior)
{
int toughtotal = 0;
int nimbletotal = 0;
int smarttotal = 0;
int skeptictotal = 0;
int mystictotal = 0;
int cursedtotal = 0;
int brutetotal = 0;
int shreddertotal = 0;
int vanillatotal = 0;
int typetotal = 0;
int class_num = 0;
int class_total[6];
int total = 0;
hero *type[3][6] = {{0},{0}};
hero *smart[3][6] = {{0},{0}};
hero *nimble[3][6]= {{0},{0}};
hero *tough[3][6]= {{0},{0}};   
hero *skeptic[]= {0};
hero *mystic[]= {0};
hero *cursed[]= {0};
hero *brute[]= {0};
hero *shredder[]= {0};
hero *vanilla[]= {0};

//This is a sample of assigning "curr[totalwarrior]' into its respective type array.
if ((encoding&128)==1 && (encoding&64)==1)
{
    smart[smarttotal][class_num] = curr[totalwarrior]; //class_num is 0 as of this moment. 
    total = smarttotal;
    type[total][class_num] = smart[smarttotal][class_num];
    smarttotal++;
    printf("\n::|Smart Type::|\n");
}
/*There will be 2 more of this code (above) since there will be 3 types array*/

//This is assigning "type[total][class_num]" into its respective class array.

if ((encoding&32)==1)
{
    class_num = 1;
    type[total][class_num] = vanilla[vanillatotal];
    class_total[1] = vanillatotal;
    vanillatotal++; //Vanilla
    printf("\n::|Vanilla Class::|\n");
}
/*There will be 5 more of this code (above) since there will be 6 class array*/

The seach_show has to use total, class_num, type[total][class_num],class_total

seach_show必须使用total,class_num,type [total] [class_num],class_total

2 个解决方案

#1


1  

I'm presuming all the variables defined at the start of binarycheck are the ones that you will need to pass to both functions.

我假设在binarycheck开始时定义的所有变量都是你需要传递给两个函数的变量。

So why not create a structure like this:

那么为什么不创建这样的结构:

struct Parameters {
  int toughtotal;
  int nimbletotal;
  int smarttotal;
  int skeptictotal;
  int mystictotal;
  int cursedtotal;
  int brutetotal;
  int shreddertotal;
  int vanillatotal;
  int typetotal;
  int class_num;
  int class_total[6];
  int total;
  hero *type[3][6];
  hero *smart[3][6];
  hero *nimble[3][6];
  hero *tough[3][6];
  hero *skeptic[];
  hero *mystic[];
  hero *cursed[];
  hero *brute[];
  hero *shredder[];
  hero *vanilla[];
};

You will then need a function like init_parameters(struct Parameters* p) which will initialize each member of your structure as you have done at the start of binarycheck.

然后,您将需要一个类似init_parameters(struct Parameters * p)的函数,它将初始化结构的每个成员,就像您在binarycheck开始时所做的那样。

Finally called you functions like this:

最后叫你这样的功能:

void binarycheck(struct Parameters *p, int encoding, product *curr[], int totalcurr)
void search_show(struct Parameters *p)

#2


1  

This may be way off. I'm still not certain of what you're asking. But my suggestion needs better formatting than comments provide.

这可能有点远了。我还不确定你在问什么。但我的建议需要比评论提供更好的格式。

It sounds to me like you're just looking for another structure that you want to pass between functions. Create it, pass it into binarycheck to fill, and then pass it into search_show to make use of. Your structure would include what you're referring to as "all the variables used by binaryCheck". Your code would go something like this:

听起来像你只是在寻找你想要在功能之间传递的另一种结构。创建它,将其传递给binarycheck进行填充,然后将其传递给search_show进行使用。您的结构将包括您所指的“binaryCheck使用的所有变量”。你的代码会是这样的:

typedef struct binary_check_values {
    int toughtotal;
    int nimbletotal;
    int smarttotal;
    int skeptictotal;
    int mystictotal;
    int cursedtotal;
    int brutetotal;
    int shreddertotal;
    int vanillatotal;
    int typetotal;
    int class_num;
    int class_total[6];
    int total;
    hero *type[3][6];
    hero *smart[3][6];
    hero *nimble[3][6];
    hero *tough[3][6];
    hero *skeptic[];
    hero *mystic[];
    hero *cursed[];
    hero *brute[];
    hero *shredder[];
    hero *vanilla[];
} binaryCheckValues;

binaryCheckResults(int encoding, hero *curr[], int totalWarrior, binaryCheckValues *values) {
    values->toughtotal = 0;
    values->nimbletotal = 0;
    /* etc */
}
/* ... other code ... */
search_show(values);

#1


1  

I'm presuming all the variables defined at the start of binarycheck are the ones that you will need to pass to both functions.

我假设在binarycheck开始时定义的所有变量都是你需要传递给两个函数的变量。

So why not create a structure like this:

那么为什么不创建这样的结构:

struct Parameters {
  int toughtotal;
  int nimbletotal;
  int smarttotal;
  int skeptictotal;
  int mystictotal;
  int cursedtotal;
  int brutetotal;
  int shreddertotal;
  int vanillatotal;
  int typetotal;
  int class_num;
  int class_total[6];
  int total;
  hero *type[3][6];
  hero *smart[3][6];
  hero *nimble[3][6];
  hero *tough[3][6];
  hero *skeptic[];
  hero *mystic[];
  hero *cursed[];
  hero *brute[];
  hero *shredder[];
  hero *vanilla[];
};

You will then need a function like init_parameters(struct Parameters* p) which will initialize each member of your structure as you have done at the start of binarycheck.

然后,您将需要一个类似init_parameters(struct Parameters * p)的函数,它将初始化结构的每个成员,就像您在binarycheck开始时所做的那样。

Finally called you functions like this:

最后叫你这样的功能:

void binarycheck(struct Parameters *p, int encoding, product *curr[], int totalcurr)
void search_show(struct Parameters *p)

#2


1  

This may be way off. I'm still not certain of what you're asking. But my suggestion needs better formatting than comments provide.

这可能有点远了。我还不确定你在问什么。但我的建议需要比评论提供更好的格式。

It sounds to me like you're just looking for another structure that you want to pass between functions. Create it, pass it into binarycheck to fill, and then pass it into search_show to make use of. Your structure would include what you're referring to as "all the variables used by binaryCheck". Your code would go something like this:

听起来像你只是在寻找你想要在功能之间传递的另一种结构。创建它,将其传递给binarycheck进行填充,然后将其传递给search_show进行使用。您的结构将包括您所指的“binaryCheck使用的所有变量”。你的代码会是这样的:

typedef struct binary_check_values {
    int toughtotal;
    int nimbletotal;
    int smarttotal;
    int skeptictotal;
    int mystictotal;
    int cursedtotal;
    int brutetotal;
    int shreddertotal;
    int vanillatotal;
    int typetotal;
    int class_num;
    int class_total[6];
    int total;
    hero *type[3][6];
    hero *smart[3][6];
    hero *nimble[3][6];
    hero *tough[3][6];
    hero *skeptic[];
    hero *mystic[];
    hero *cursed[];
    hero *brute[];
    hero *shredder[];
    hero *vanilla[];
} binaryCheckValues;

binaryCheckResults(int encoding, hero *curr[], int totalWarrior, binaryCheckValues *values) {
    values->toughtotal = 0;
    values->nimbletotal = 0;
    /* etc */
}
/* ... other code ... */
search_show(values);