0

I have around 14 void functions that contains processes needed in order for my program to work

and all the 14 functions share the same variables so I thought of making them into static Global.

After putting <stdio.h> and all the other headers needed, I have 2 typedef struct and after that, I have put 11 static int variables and 3 static struct variables.

I have checked every single function if the struct variables have been storing the data properly, and apparently, only the void function that is first called in int main() stores the correct data into the struct variable.

When the 2nd void function is called, the global variables from the 1st void function contains no data at all.

Can anyone tell me if using the global variables for multiple functions to work is wrong?

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

typedef struct hero_data{
//... data
}hero;

typedef struct item_data{
//.... data
}item;

int totalwarrior = 0;
int totalitem = 0;
int toughtotal = 0;
int nimbletotal = 0;
int smarttotal = 0;
int skeptictotal[3] = {0};
int mystictotal[3] = {0};
int cursedtotal[3] = {0};
int brutetotal[3] = {0};
int shreddertotal[3] = {0};
int vanillatotal[3] = {0};
int typetotal = 0;
int typenum = 0;
int typechoice = 0;
int classchoice = 0;
static item curr3[10000];
static hero curr[10000];
static hero curr2[10000];
static hero smart[1][10000];
static hero nimble[1][10000];
static hero tough[1][10000];    
static hero type[3][10000];
static hero skeptic[3][10000];
static hero mystic[3][10000];
static hero cursed[3][10000];
static hero brute[3][10000];
static hero shredder[3][10000];
static hero vanilla[3][10000];
static hero player1;
static hero player2;

int randbetween(int max, int min)
{
//... functioning
}

void mygets(char *name, int len, FILE * stream)
{
//... functioning
}

void available_hero(hero Class[3][10000],int typenum, int classtotal[],int classcode) //Shows the available hero based on Player's choice
{
//... functioning
}

void detail_hero(hero curr[3][10000],int classtotal[],int typenum)
{
//....functioning
}

void detail_item(item curr[10000],int whatitem)
{
//functioning
}

void pointer_conversion(hero *a, hero curr[10000], int total)
{
//....functioning
}

void TDpointer_conversion(hero *a, hero curr[3][10000], int total,int typenum)
{
//....functioning
}

void pointer_conversion2(item *a, item curr3[], int total)
{
//...functioning
}

void OD_conversion(int a[], int curr[],int typenum)
{
//....functioning
}

void TD_conversion(hero a[3][10000],hero curr[3][10000],int typetotal, int typenum, int typetotal2)
{
//....functioning
}

void TD_conversion2(hero a[3][10000],hero curr[3][10000],int typetotal[], int typenum, int typetotal2[])
{
//....functioning
}

void TD_conversion_class(hero a[1][10000],hero curr[3][10000],int classtotal[3], int typenum, int typetotal)
{
//....functioning
}

void binarycheck(int encoding, hero *dummy, int totalwarrior)
{
//....functioning
}

void check_compare_item(hero player)
{
//....functioning
}

void create_player(hero player)
{
//....functioning
}    

void load_hero(hero curr[])
{
//....functioning
}

int main()
{
load_hero(curr);
load_item(curr3);
create_player(player1);
printf(""\n --> %s <---\n",player1.name); //struct hero contains the variable "name"
// Nothing gets printed while when I try to print the name within Create_player function, it works.
check_compare_item(player1);
}
4

2 回答 2

1

当您按值传递参数并且您在被调用函数内对其进行的更改时,调用者将看不到调用者(被调用者正在操作数据的副本)。

解决方案:通过指针发送数据。

更好的是:完全避免使用静态变量。在这种情况下,它们提供了常规变量没有的什么好处?

于 2013-10-06T07:24:06.080 回答
0

如果使用全局变量,为什么需要将它们传递给函数?您可以直接在函数中使用它们,而无需通过 args 传递它们。传递参数是这里的问题。

您是否使用 static 关键字将它们保密?否则,请避免静电。

于 2013-10-06T07:31:24.117 回答