分类: LINUX
2011-09-01 00:28:27
void GetMemory( char *p ) { p = (char *) malloc( 100 ); } void Test( void ) { char *str = NULL; GetMemory( str ); strcpy( str, "hello world" ); printf( str ); } |
char *GetMemory( void ) { char p[] = "hello world"; return p; } void Test( void ) { char *str = NULL; str = GetMemory(); printf( str ); } |
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 Test( void ) { char *str = (char *) malloc( 100 ); strcpy( str, "hello" ); free( str ); ... //省略的其它语句 } |
char *str = NULL; GetMemory( str ); |
char p[] = "hello world"; return p; |
*p = (char *) malloc( num ); |
if ( *p == NULL ) { ...//进行申请内存失败处理 } |
char *str = (char *) malloc(100); |
str = NULL; |
swap( int* p1,int* p2 ) { int *p; *p = *p1; *p1 = *p2; *p2 = *p; } |
swap( int* p1,int* p2 ) { int p; p = *p1; *p1 = *p2; *p2 = p; } |