运行Test函数会有什么样的结果

1

void GetMemory(char *p) 
{ 
	p = (char *)malloc(100); 
}

void Test(void) 
{ 
	char *str = NULL; 
	GetMemory(str); 
	strcpy(str, "hello world"); 
	printf(str); 
}
int main()
{
	Test();
	return 0;
}


2

char *GetMemory(void) 
{ 
	char p[] = "hello world"; 
	return p; 
} 

void Test(void) 
{ 
	char *str = NULL; 
	str = GetMemory(); 
	printf(str); 
}

int main()
{
	Test();
	return 0;
}


3

void GetMemory(char **p, int num) 
{ 
	*p = (char *)malloc(num); 
} 

void Test(void) 
{ 
	char *str = NULL; 
	GetMemory(&str, 100); 
	strcpy(str, "hello"); 
	printf(str); 
}


int main()
{
	Test();
	return 0;
}


4

void Test(void) 
{ 
	char *str = (char *) malloc(100); 
	strcpy(str, "hello"); 
	free(str); 
	if(str != NULL) {
		strcpy(str, "world"); 
		printf(str); 
	} 
}


int main()
{
	Test();
	return 0;
}





版权声明:本文为troy002原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。