【发布时间】:2013-10-06 07:08:51
【问题描述】:
我有大约 14 个void functions,其中包含我的程序运行所需的进程
所有 14 个函数共享相同的变量,所以我想将它们变成静态全局。
在放置 <stdio.h> 和所有其他所需的标头之后,我有 2 个 typedef struct ,之后,我放置了 11 个 static int 变量和 3 个 static struct 变量。
如果struct 变量已正确存储数据,我已经检查了每个函数,显然,只有在int main() 中首次调用的void function 将正确的数据存储到struct 变量中。
当调用第二个void function 时,来自第一个void function 的全局变量根本不包含任何数据。
谁能告诉我使用多个函数的全局变量是否是错误的?
#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);
}
【问题讨论】:
-
为什么不发布代码?否则不可能给出答案
-
不要在编辑代码时删除它。它取消了我当时正在写的答案。
-
您发布的内容没有明显缺陷。你确定你的所有函数都在同一个
.c文件中吗?
标签: c struct global-variables