When I call the "InitAnimation" function, I pass the the address of my object RG. When I assign the "animationName" field of that object, I can successfully print that field. But when I return to main and call the function "ReportAnimation", I am unable to print that value and my program crashes ? How come when I assigned that objects field it is not changed globally but only in the local function ?
I have tried allocating memory for the animationName field as well but that does not work.
struct Frame {
char* frameName;
struct Frame* pNext;
};
typedef struct {
char* animationName;
struct Frame* frames;
}Animation;
int main(void) {
char response;
BOOL RUNNING = TRUE;
Animation RG;
InitAnimation(&RG);
while (RUNNING) {
printf("MENU\n Enter 1 to ReportAnimation\n");
scanf("%c", &response);
switch (response) {
case '1':InsertFrame(&RG);
break;
}
}
return 0;
}
void InitAnimation(Animation* pointer) {
pointer = (Animation*)malloc(sizeof(Animation));
char* input;
input = (char*)malloc(sizeof(input));
printf("Please enter the Animation name:");
fgets(input, 32, stdin);
//pointer->animationName = (char*)malloc(sizeof(char)*10);
//Setting animation name
pointer->animationName = input;
//This print function works
printf("\nThe name is %s", pointer->animationName);
}
void ReportAnimation(Animation* pointer) {
//This print function does not work
printf("Animation name is %s\n", pointer->animationName);
}
I want the initAnimation function to change the field of the Animation struct and I want the reportAnimation function to print out the field, proving its changed