I am trying to understand pointers and I've got this simple example
void third(char ****msg) {
***msg = malloc(5 * sizeof (char));
printf("\nthe msg in third is :%s ", ***msg);
strcpy(***msg, "third");
printf("\nthe msg in third after is: %s", ***msg);
// free(***msg);
}
void change(char***msg) {
**msg = malloc(5 * sizeof (char));
printf("\nthe msg in change is :%s ", **msg);
strcpy(**msg, "change");
printf("\nthe msg in change after is: %s", **msg);
third(&msg);
// free(**msg);
}
void test(char ** msg) {
*msg = malloc(5 * sizeof (char));
printf("\n the msg in test is: %s", *msg);
strcpy(*msg, "test");
printf("\nthe msg in test after is: %s\n", *msg);
change(&msg);
free(*msg);
}
int main(int argc, char** argv) {
char * msg;
test(&msg);
printf("\nthe msg back in main is: %s", msg);
}
I could say it is working fine, but could you tell me when and how I need to free the allocated memory, because if I remove the // from functions change and third and run it I am having errors. And is there a way to get the content of the message in the first print statement of each function - see the otuput:
the msg in test is:
the msg in test after is: test
the msg in change is :0��
the msg in change after is: change
the msg in third is :P��
the msg in third after is: third
the msg back in main is:
Is there a way to get the msg in change is : test and then the msg in third is : change
Just forget about that program, there's just too much wrong with it:
And so on. Here's a fixed version:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void third(char** msg) {
const char str[] = "third";
*msg = realloc(*msg, sizeof(str));
printf("the msg in third is :%s\n", *msg);
strcpy(*msg, str);
printf("the msg in third after is: %s\n", *msg);
}
void change(char** msg) {
const char str[] = "change";
*msg = realloc(*msg, sizeof(str));
printf("the msg in change is :%s\n", *msg);
strcpy(*msg, str);
printf("the msg in change after is: %s\n", *msg);
third(msg);
}
void test(char** msg) {
const char str[] = "test";
*msg = malloc(sizeof(str));
printf("the msg in test is just garabage at this point, no need to print it.\n");
strcpy(*msg, str);
printf("the msg in test after is: %s\n", *msg);
change(msg);
}
int main(int argc, char** argv) {
char* msg;
test(&msg);
printf("the msg back in main is: %s\n", msg);
free(msg);
}