function - How to manipulate the Global Variable in C -
i have multiple void functions
relies on each individual output of functions since there multiple variables (that same throughout code), each functions' output "stored" them , passed another.
so, decided make variables global variables making them static ....
right after necessary #include...
codes.
i able utilize functions (14 functions in total,all void
) calling 4 of them (each functions, after processing own function, passes result function , after series of passing, 4 of them needed called in int main()
)
now, created void function
requires global variables parameter since void function
relies on data other functions "copied , put" global variables declared earlier. (which found not working, since heard storing data global variables not possible.)
can teach me if there other way create series of functions requires output of each individual functions?
i checked if variables stored properly, tried using printf
method right after #3 process. found out nothing gets printed when expected value struct data
printed.
ex:
typedef struct database{ //... variables }data; typedef struct itembase{ //... variables }item; static data user1; static data user2; static data *pointer[10000]; static item *pointer2[10000]; static item current[10000]; //shares same value of bracket *pointer2 static data sectiona[1][10000]; static data sub_section[3][10000]; static int datacounter = 0; //..will put inside bracket of *pointer static int itemcounter = 0; //..will put inside bracket of *pointer2 static int typenum = 0; ..will put inside first bracket of sections , subsections static int section_count = 0; //..will put inside second bracket of sections static int sub_section_count[3] = {0}; //..will put inside second bracket of sub_sections. [3] value of typenum. void load_data() // accepts user's input , store them struct data's variable using singly-linked list { //.... data stored *pointer[datacounter] binarycheck(pointer[datacounter]->encoding,*pointer,datacounter); //.... `typedef struct` of data contains 12 variables. after storing 12 variables, datacounter ++ , program still continue accept input user } void load_item() { //.... item stored *pointer2[itemcounter] memcpy(¤t[itemcounter],pointer2[itemcounter],sizeof(item)); } void binarycheck(data encoding,data *pointer,int datacounter) { if ((encoding&128)==128){ typenum = 3; memcpy(§iona[typenum][section_count],pointer,sizeof(data)); sub_sectiona[typenum][sub_section_count[typenum]] = sectiona[typenum[section_count]; section_count++; sub_section_count++; } } void askitem(data user) { // tried putting `printf(" %s user1 data#1",user1.firstdata);` , works fine. // ask user's selection on item // if item found, content of item modify data of variable of `user` } void askinput(data user) { int whattype = 0; int whatsub = 0; printf("what type want?: \n); scanf("%d",&whattype); if (whattype == 1) {typenum = 1;} printf("what sub type want?: \n); scanf("%d",&whatsub); if (whatsub == 1) { user = sub_sectiona[typenum][sub_section_count[typenum]];} askitem(user); } void final_print(data user, data user2) { printf("%d\n",user.adata); printf("%d\n",user2.adata); } int main() { load_data(); load_item(); askinput(user1); //tried putting `printf(" %s user1 data#1",user1.firstdata);` nothing shows. askinput(user2); //nothing shows final_print(user1,user2); //nothing shows }
take @ function:
void askinput(data user)
here pass user
value function. when pass value, function receives copy of variable. changes make inside body of function affect copy. not visible caller's variable.
instead need pass reference. in c means passing pointer variable:
void askinput(data *user)
inside body of function need de-reference pointer access members. use ->
rather .
refer members.
and when call function need pass pointer variable. call becomes:
askinput(&user1);
frankly not understand why using global variables here @ all. it's preferable pass parameters otherwise find struggling keep track of different version of variable meant working on.
finally, have written entire program , trying debug specific problem in context of entire program confusing you. should have cut down 10 or 20 line simple reproduction. being able in future make life easier you.
Comments
Post a Comment