So I am just making a quick game to help me understand programming in c++. Basically I made a pointer to a struct called player1 in the main method. After I initialized it and gave it some memory to use I decided to make a method to display the values of this specific pointer. Now I think this just shows that maybe I don't understand pointers like I should but When I try and access the pointer that I made in main it doesn't exist. in code i should be able to type player1->health to get the health in the method, but player1 doesn't exist in the methods scope.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct enemy{
char name[120];
float health;
int magic;
int level;
} ene;
typedef struct player{
char name[120];
float health;
int magic;
int level;
int exp;
} play;
void startGame();
void saveGame();
void displayHud();
int main(int argc, const char * argv[]) {
// insert code here...
// check if there is a save game
// check if save game
// initailize the player with 100 health and magic
play *player1;
player1 = (play *) malloc(sizeof(play));
player1->health = 100;
player1->level = 0;
player1->magic = 100;
displayHud();
startGame();
free(player1);
return 0;
}
void displayHud(){
printf("Health: %d Magic: %d Exp: %d", player1->health); //Doesn't exist
}