Use a pointer to point to the global variable. Change the pointer value. Thats it
Answer from Vamsavardhana Vijay on Stack OverflowUse a pointer to point to the global variable. Change the pointer value. Thats it
The problem with the first is that you pass the variable as an argument to the function, so when the function modifies the variable it's only modifying its own local copy and not the global variable. That is, the local variable i shadows the global variable i.
Not to mention that you don't actually declare the argument properly, so your program should not even compile.
I used to know how to do this, but I've forgotten. I am currently working with a little terminal based game project of mine that requires that global variables be changed so they can be called by multiple functions, and sometimes modified in those functions for later use by one or more functions.
How do you change a global variable?
Edit 2: I solved my problem using pointers. For anyone wondering how to change a global variable (globally), you can use pointers.
Thanks for all the help everyone.
EDIT: Someone asked for the code, so I am including it here. The global variable I need is for the positionx and positiony variables. I am making a little mario type platformer game in terminal and I want to track the position in the 2d space using x and y positions that regularly get updated by different functions.
Right now the problem is the function gravity and the function movement are using two different version of the variables instead of the same one.
I used an array to store the data because I thought I was being clever and that would change the variable globally, but nope it still only does it locally.
Here is the code:
//START OF CODE
include <cs50.h>
include <stdio.h>
include <stdlib.h>
include <unistd.h>
int world[100][119]; int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
int q = 0;
int r = 0;
int length = 30;
int height = 119;
int lengthinitialized = 99;
int heightinitialized = 119;
int worldpositionx = 0;
int worldpositiony = 0;
//use an array to track position as it alters a referencable number in a global way
int globalxy[2][2];
//use array to track movements
int movement[1][1];
//win condition has to equal 1 to stop
int won[1][1];
//void draw(void);
void populateboard(void);
void placelimits(void);
void placeobjects(void);
void draw(void);
void initializeplayer(void);
void gravity(void);
void move(void);
void instructions(void);
//void clear(void);
int main(void)
{ won[0][0] = 0;
populateboard();
placelimits();
placeobjects();
initializeplayer();
draw();
while(won[0][0] != 1)
{
worldpositiony = globalxy[0][1];
worldpositionx = globalxy[1][0];
if(world[worldpositionx+1][worldpositiony] == 0)
{
while(world[worldpositionx+1][worldpositiony] == 0)
//for(i=0;i<20;i++)
{
gravity();
}
}
draw();
instructions();
move();
}}
void draw(void)
{
//Orignal draw before limited scope
for(m=0;m<length ;m++)
{
if(world[m][n]==0) { printf(" "); } else { printf("%i", world[m][n]); }
for(n=0;n<height ;n++)
{
if(world[m][n]==0)
{
printf(" ");
}
else
{
printf("%i", world[m][n]);
}
}
printf("\n");
}}
// **/
void populateboard(void) { for(i=0;i<lengthinitialized;i++) { for(j=0;j<heightinitialized;j++) { world[i][j] = 0; }
} /** * 0s represent air * 1s represent solid objects **/
}
void placelimits(void) { //Boundaries are 10 layers thick to accomodate draw distance of 10. for(k=0;l<height; l++) { for(q=0; q<10;q++) { world[q][l] = 1; world[(length -1) - q][l]= 1; } }
for(l=0;k<length;k++)
{
for(r=0;r<10;r++)
{
world[k][(height -1) - r] = 1;
world[k][r] = 1;
}
}
/**
* 0s represent air
* 1s represent solid objects
**/}
void placeobjects(void) { //test object for(o = 1; o<4; o++) { world[(o * 20)][5] = 1;
world[(o * 20)+1][5] = 1;
world[(o * 20)-1][5] = 1;
world[(o * 20)][6] = 1;
world[(o * 20)][4] = 1;
}}
void initializeplayer(void) { world[15][15] = 2; worldpositionx = 15; worldpositiony = 15; globalxy[0][1] = worldpositiony; globalxy[1][0] = worldpositionx;
}
void gravity(void) { //world[worldpositionx][worldpositiony] = 2; worldpositionx = globalxy[0][1]; //redundant line, but for clarity
if(world[worldpositionx+1][worldpositiony] == 0)
{
worldpositionx = worldpositionx +1;
globalxy[0][1] = worldpositionx;
world[worldpositionx][worldpositiony] = 2;
world[worldpositionx-1][worldpositiony] = 0;
//if position +1 is 0, drop. else end loop
}}
void move(void)
{
//sideways movement
movement[0][0] = GetInt();
worldpositiony = globalxy[0][1];
worldpositionx = globalxy[1][0];
world[worldpositionx][worldpositiony] = 2;
if((movement[0][0] == 4) && (world[worldpositionx][worldpositiony-1] == 0))
{
worldpositiony = worldpositiony -1;
globalxy[0][1] = worldpositiony;
world[worldpositionx][worldpositiony] = 2;
world[worldpositionx][worldpositiony+1] = 0;
}
else if((movement[0][0] == 6) && (world[worldpositionx][worldpositiony+1] == 0))
{
worldpositiony = worldpositiony +1;
globalxy[0][1] = worldpositiony;
world[worldpositionx][worldpositiony] = 2;
world[worldpositionx][worldpositiony-1] = 0;
}
else
{
printf("An error occured \n");
}//use a variable to track whether to move up down, or sideways. using get int. }
void instructions(void)
{
printf("Press 4 and 6 to move, and press 8 to jump, and press the 7 key to mega jump. Please input your move. \n");
}
/**
-
Clears screen using ANSI escape sequences.
void clear(void) { printf("\033[2J"); printf("\033[%d;%dH", 0, 0); } */
//global variable is not being treated as a global variable. I need to to change it another way. With correct replacement. //pointers seem to be one way to do this. point to the variable that the global variable pulls. and when I want to change it change the value of the things pointed to. //or else find an elegant way to track both of these with local variables. maybe merge movement and gravity so its on the same level.
//END of CODE
If there is better way to show this code, please let me know.
Changing a global variable's value with a function with the variable passed as an argument
Change value of global variable, C - Stack Overflow
How can you modify global variables that are used in functions that are being called with ccall?
c++ - Changing global variables in functions - Stack Overflow
Videos
As you are passing SIZE_ARRAY as parameter in your function remove_unimportant_words, This is not using as global in that function any more.. So Global SIZE_ARRAY remains same.
You should not pass SIZE_ARRAY as parameter. Hope your code will work as expected.
void remove_unimportant_words(char word[MAX], char list[SIZE_ARRAY][MAX] , int j, int i)
{
...
If SIZE_ARRAY is a global variable as you stated, you need not pass another integer parameter to the function remove_unimportant_words().
The SIZE_ARRAY you passed into remove_unimportant_words() effectively shadows the global variable with a local copy of a unrelated, newly allocated variable also under the name SIZE_ARRAY which will be deallocated as the function returns.
In essence:
void remove_unimportant_words(char word[MAX], int SIZE_ARRAY, char list[SIZE_ARRAY][MAX] , int j, int i);
the int SIZE_ARRAY parameter there shows no relation to the actual global SIZE_ARRAY variable and should be removed as so that code within the function body references the global SIZE_ARRAY variable directly.
You could potentially also use addresses and pointers to pass the variable by reference if you mean to merely pass SIZE_ARRAY from one function to another.
As a side note, are you sure the code you provided compiles? I see a lot of syntax that doesn't seem valid...
The code:
void hide_global(){
glob = 0;
cout << "In hide global is: " << glob << endl;
}
Is not declaring any new variables, it is assigning to an already existing variable called glob, which is your global variable. To declare a new variable in the function you would need to specifiy the data type as well, like this:
void hide_global(){
int glob = 0;
cout << "In hide global is: " << glob << endl;
}
You are not creating a local variable in any your hide_global function, you are simply changing the global variable. To create a new local version, do this:
void hide_global(){
int glob = 0; //note the inclusion of the type to declare a new variable
cout << "In hide global is: " << glob << endl;
}