malloc的问题,太诡异了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void upcase(char *oldstr, char *newstr);
int main(void)
{
char *string = NULL;
upcase("Hello", string);
printf("str1=%s \n", string);
free(string);
upcase("Goodbye", string);
printf("str2=%s\n", string);
free(string);
return 0;
}
void upcase(char *oldstr, char *newstr)
{
int i;
newstr = malloc(strlen(oldstr) + 1);
strcpy(newstr, oldstr);
printf("%s\n",newstr);
for (i = 0; i < strlen(newstr); i++)
{
newstr[i] = toupper(newstr[i]);
}
return;
}
执行结果是:
Hello
str1=(null)
Goodbye
str2=(null)
为什么是这样子?