void test1() { char string[10]; char* str1 = "0123456789"; strcpy( string, str1 ); } |
void test2() { char string[10], str1[10]; int i; for(i=0; i<10; i++) { str1[i] = 'a'; } strcpy( string, str1 ); } |
void test3(char* str1) { char string[10]; if( strlen( str1 ) <= 10 ) { strcpy( string, str1 ); } } |
void strcpy( char *strDest, char *strSrc ) { while( (*strDest++ = * strSrc++) != ‘\0’ ); } |
void strcpy( char *strDest, const char *strSrc ) //将源字符串加const,表明其为输入参数,加2分 { while( (*strDest++ = * strSrc++) != ‘\0’ ); } |
void strcpy(char *strDest, const char *strSrc) { //对源地址和目的地址加非0断言,加3分 assert( (strDest != NULL) && (strSrc != NULL) ); while( (*strDest++ = * strSrc++) != ‘\0’ ); } |
//为了实现链式操作,将目的地址返回,加3分! char * strcpy( char *strDest, const char *strSrc ) { assert( (strDest != NULL) && (strSrc != NULL) ); char *address = strDest; while( (*strDest++ = * strSrc++) != ‘\0’ ); return address; } |
{ assert( strt != NULL ); //断言字符串地址非0 int len; while( (*str++) != '\0' ) { len++; } return len; } |
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; } |