Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to manipulate the Global Variable in C
    text
    copied!<p>I have multiple <code>void functions</code> that relies on the each individual output of the functions since there are multiple variables (that are the same throughout the code), where each functions' output will be "stored" to them and be passed to another.</p> <p>So, I decided to make those variables into global variables by making them <code>static ....</code> right after all the necessary <code>#include...</code> codes.</p> <p>I was able to utilize all functions (14 functions in total,all <code>void</code>) by only calling four of them (Each functions, after processing its own function, passes the result into another function and after series of passing, only four of them are needed to be called in <code>int main()</code>)</p> <p>Now, I created another <code>void function</code> that requires the global variables as its parameter since that <code>void function</code> relies on the data that all the other functions "copied and put" into the global variables declared earlier. (Which I found is not working, since I heard that storing data into global variables is not possible.)</p> <p>Can anyone teach me if there is any other way to create series of functions which requires output of each individual functions?</p> <p>I checked if the variables were stored properly, so I tried using <code>printf</code> method right after the #3 process. I found out nothing gets printed when I expected a value from the <code>struct data</code> to be printed.</p> <hr> <p>Ex:</p> <pre><code>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 the bracket with *pointer2 static data sectionA[1][10000]; static data sub_section[3][10000]; static int datacounter = 0; //..will be put inside the bracket of *pointer static int itemcounter = 0; //..will be put inside the bracket of *pointer2 static int typenum = 0; ..will be put inside the first bracket of all the sections and subsections static int section_count = 0; //..will be put inside the second bracket of all sections static int sub_section_count[3] = {0}; //..will be put inside the second bracket of all sub_sections. The [3] will be the value of the typenum. void load_data() // Accepts User's input and store them into struct data's variable using singly-linked list { //.... All data will be stored to *pointer[datacounter] binarycheck(pointer[datacounter]-&gt;encoding,*pointer,datacounter); //.... The `typedef struct` of data contains 12 variables. After storing 12 variables, datacounter will be ++ and the program will still continue to accept input from the user } void load_item() { //.... All item will be stored to *pointer2[itemcounter] memcpy(&amp;current[itemcounter],pointer2[itemcounter],sizeof(item)); } void binarycheck(data encoding,data *pointer,int datacounter) { if ((encoding&amp;128)==128){ typenum = 3; memcpy(&amp;sectionA[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);` and it works perfectly fine. // Ask for user's selection on item // If the item is found, then the content of that item will modify the data of the variable of `user` } void askinput(data user) { int whattype = 0; int whatsub = 0; printf("What type do you want?: \n); scanf("%d",&amp;whattype); if (whattype == 1) {typenum = 1;} printf("What Sub type do you want?: \n); scanf("%d",&amp;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);` but nothing shows. askinput(user2); //Nothing shows final_print(user1,user2); //Nothing shows } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload