세그먼테이션이 일어납니다.
글쓴이: nayana / 작성시간: 목, 2005/01/27 - 2:03오후
1 #include <cstdio> 2 #include <cstring> 3 4 char* ClarifyNumber( char *str, char *buf ) 5 { 6 int len; 7 char *p = NULL; 8 9 if ( *str == '\0' ) 10 { 11 *buf = '\0'; 12 13 return( (char *)NULL ); 14 } 15 16 /* strip space backward */ 17 p = str + strlen( str ) - 1; 18 19 for ( ; *p == ' ' ; p-- ) ; 20 21 *( ++p ) = '\0'; 22 23 /* strip space forward */ 24 for ( ; *str == ' ' || *str == '0'; str++ ); 25 26 p = buf; 27 28 if ( *str == '+' ) *str++; 29 if ( *str == '-' ) *buf++ = *str++; 30 31 if ( ( len = strlen( str ) ) <= 0 ) 32 { 33 *buf = '\0'; 34 return( (char *)NULL ); 35 } 36 37 while ( ( *buf++ = *str++ ) ) 38 { 39 if ( --len && ( len % 3 ) == 0 ) 40 *buf++= ','; 41 } 42 43 *buf = '\0'; 44 45 return( p ); 46 } 47 48 int main ( void ) 49 { 50 char* s = "1234"; 51 char buf[ 1024 ] = { 0, }; 52 53 char* abc = ClarifyNumber( s, buf ); 54 printf( "%s\n", abc ); 55 56 return 0; 57 }
21라인에서 세그먼테이션이 일어나는데...왜그러지요?
Forums:
- 함수 호출부분에서 "char *str"이 실제로 메모리가 확보된 건지
- 함수 호출부분에서 "char *str"이 실제로 메모리가 확보된 건지 확인해보세요. 만약 "char *str="abc";"형태로 선언했다면.. 당근 세그폴트 납니다.
- 에공 다시보니 50번 라인에서 잘못 선언하셨네요.. 그렇게 하면 안돼요...
이렇식으로 하세요..
댓글 달기