请问该程序运行main函数会有什么样的结果,为什么?
void GetMemory(char *p){
p = (char *)malloc(100);
}
void main(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
我感觉是屏幕上输出:hello world
但不知道是为什么
谢谢大家!
而且我把这段代码复制到VC上运行不了
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void main(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
错误为:
--------------------Configuration: japan - Win32 Debug--------------------
Compiling...
p139.cpp
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(70) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(70) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(75) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(75) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(76) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(76) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(77) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(77) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(78) : error C2018: unknown character '0xa1'
C:\Documents and Settings\Administrator\桌面\新建文件夹 (3)\japan\p139.cpp(78) : error C2018: unknown character '0xa1'
执行 cl.exe 时出错.
japan.exe - 1 error(s), 0 warning(s)
[ 本帖最后由 fz19910125 于 2011-2-15 14:03 编辑 ]