#include "stdio.h"
#include "malloc.h"
//When you want to allocate memory in function inside, you should use the indirect pointer to manipulate the pointer.
// #define SE
#ifdef SE
void test_malloc(char** addr)
{
(*addr) = "Can U C me?"; //addr is the address of ad, So, (*addr) is just ad!
return;
}
void main(void)
{
char * ad;
test_malloc(&ad); //"&ad" is address of ad! So, the type of &ad is "char **"
//can output "Can U C me?"
printf("%s", ad);
return;
}
#endif
#ifndef SE
void test_malloc(char* addr)
{
addr = "Can U C me?";
return;
}
void main(void)
{
char * ad;
test_malloc(ad);
//can not output and program will generate exception.
printf("%s", ad);
return;
}
#endif