关于函数返回指针的 问题!!
发表于 : 2008-01-06 20:44
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *test( int num )
{
char *p = (char *)malloc( sizeof( char )*num );
return p;
}
int main( void )
{
char *p = NULL;
p = test( 10 ); //故意是申请的空间小于要拷贝的内容
strcpy( p , "hello! world rutrueiyteiy " );
printf("string *p: %s\n",p);
while( *p != '\0' )
printf("%c ",*p++);
return 0;
}
按照书上说的肯定是 不能 正确运行的 阿 ,但是在ubuntu上面:
程序能够正常运行:
qc@qc-laptop:/home/q$ cc -o t t.c
qc@qc-laptop:/home/q$ ./t
string *p: hello! world rutrueiyteiy
h e l l o ! w o r l d r u t r u e i y t e i y qc@qc-laptop:/home/q$
请问:不是 说不能返回指向动态内存的指针马?
还有 很明显申请的内存空间小于需要的空间???
#include<stdlib.h>
#include<string.h>
char *test( int num )
{
char *p = (char *)malloc( sizeof( char )*num );
return p;
}
int main( void )
{
char *p = NULL;
p = test( 10 ); //故意是申请的空间小于要拷贝的内容
strcpy( p , "hello! world rutrueiyteiy " );
printf("string *p: %s\n",p);
while( *p != '\0' )
printf("%c ",*p++);
return 0;
}
按照书上说的肯定是 不能 正确运行的 阿 ,但是在ubuntu上面:
程序能够正常运行:
qc@qc-laptop:/home/q$ cc -o t t.c
qc@qc-laptop:/home/q$ ./t
string *p: hello! world rutrueiyteiy
h e l l o ! w o r l d r u t r u e i y t e i y qc@qc-laptop:/home/q$
请问:不是 说不能返回指向动态内存的指针马?
还有 很明显申请的内存空间小于需要的空间???