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.
How do you update a global variable in c?
Changing a global variable's value with a function with the variable passed as an argument
How can you modify global variables that are used in functions that are being called with ccall?
Global Variables - With Fucntions.
Example for global variables
global MyVar := 1337
MsgBox % MyFunc("Leet")
MyFunc(string)
{
return string " = " MyVar
}or
MyVar := 1337
MsgBox % MyFunc("Leet")
MyFunc(string)
{
global MyVar
return string " = " MyVar
} More on reddit.com Videos
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.