试题4:void GetMemory(char *p ){p = (char *) malloc(100 );}void Test(void ){char *str =
试题4:
void GetMemory(char *p )
{
p = (char *) malloc(100 );
}
void Test(void )
{
char *str = NULL;
GetMemory(str );
strcpy(str, "hello world" );
printf(str );
}
试题4:
void GetMemory(char *p )
{
p = (char *) malloc(100 );
}
void Test(void )
{
char *str = NULL;
GetMemory(str );
strcpy(str, "hello world" );
printf(str );
}
试题4:
void GetMemory(char *p )
{
p = (char *) malloc(100 );
}
void Test(void )
{
char *str = NULL;
GetMemory(str );
strcpy(str, "hello world" );
printf(str );
}
试题6:
void GetMemory(char **p, int num )
{
*p = (char *) malloc(num );
}
void Test(void )
{
char *str = NULL;
GetMemory(&str, 100 );
strcpy(str, "hello" );
printf(str );
试题5:
char *GetMemory(void )
{
char p[] = "hello world";
return p;
}
void Test(void )
{
char *str = NULL;
str = GetMemory();
printf(str );
}
试题5:
char *GetMemory(void )
{
char p[] = "hello world";
return p;
}
void Test(void )
{
char *str = NULL;
str = GetMemory();
printf(str );
}
试题6:
void GetMemory(char **p, int num )
{
*p = (char *) malloc(num );
}
void Test(void )
{
char *str = NULL;
GetMemory(&str, 100 );
strcpy(str, "hello" );
printf(str );
}
请指出下列程序中的错误并且修改
void GetMemory(char *p){
p=(char *)malloc(100);
}
void Test(void){
char *str=NULL;
GetMemory=(str);
strcpy(str,"hello world");
printf(str);
}
A:错误--参数的值改变后,不会传回
GetMemory并不能传递动态内存,Test函数中的str
一直都是NULL。
strcpy(str, "hello world");将使程序崩溃。
分析下面的程序:
void GetMemory(char **p,int num)
{
*p=(char *)malloc(num);
}
int main()
{
char *str=NULL;
GetMemory(&str,100);
strcpy(str,"hello");
free(str);
if(str!=NULL)
{
strcpy(str,"world");
}
printf("\n str is %s",str);
getchar();
}
问输出结果是什么?希望大家能说说原因,先谢谢了
. char *GetMemory(void) { char p[] = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } 请问运行Test 函数会有什么样的结果?
回答下面的问题(6分) (1).Void GetMemory(char **p, int num) { *p = (char *)malloc(num); } void Test(void) { char *str = NULL; GetMemory(&str, 100); strcpy(str, "hello"); printf(str); } 请问运行Test 函数会有什么样的结果?
#include <stdio.h>
#include <stdlib.h>
void getmemory(char *p)
{
p=(char *) malloc(100);
strcpy(p,"hello world");
}
int main()
{
char *str=NULL;
getmemory(str);
printf("%s/n",str);
free(str);
return 0;
}
程序崩溃,getmemory中的malloc 不能返回动态内存, free()对str操作很危险
5.char szstr[10];
strcpy(szstr,"0123456789");
产生什么结果?为什么?