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);
}
3
  • Why not post the code? It is impossible to give an answer otherwise Commented Oct 6, 2013 at 7:13
  • don't delete your code while you are editing it. It canceled my answer that I was writing at that moment. Commented Oct 6, 2013 at 7:17
  • There is no obvious flaw in what you posted. Are you sure that you have all your functions in the same .c file? Commented Oct 6, 2013 at 7:19

2 Answers 2

1

When you pass a parameter by-value and changes you make to it inside the called function will not be visible in the caller (the callee is operating on a copy of the data).

Solution: send your data via pointers.

Even better: avoid static variables altogether. What benefit do they offer in this case that a regular variable does not?

Sign up to request clarification or add additional context in comments.

Comments

0

If you use a global variable, why do you need to pass them to the function? You can directly use them from within your function without passing them through args. Passing the args is the problem here.

And are you using the static keyword to keep them private to the file? otherwise, avoid the static.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.