簡體   English   中英

靜態全局變量(C)中的錯誤

[英]Error in Static Global Variable (C)

我大約有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);
}

當您傳遞參數按值並在調用的函數中對其進行更改時,在調用方中將不可見(被調用方正在操作數據副本)。

解決方案:通過指針發送數據。

更好的是:完全避免使用靜態變量。 在這種情況下,它們提供了什么好處,而常規變量則沒有呢?

如果使用全局變量,為什么需要將它們傳遞給函數? 您可以直接在函數中使用它們,而無需通過args。 在這里傳遞參數是問題。

並且您是否使用static關鍵字將它們保留為文件私有? 否則,請避免靜電。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM