Hi Roberto, The problem is in this line in the main() function:
char *s0 = " bye 1 "; You are not allocating memory for the string, but just for the pointer. So, try this: char s0[] = " bye 1 "; And the program will work. Regards, Kayo. ----- Original Message ---- From: Roberto <[EMAIL PROTECTED]> To: [email protected] Sent: Wednesday, April 11, 2007 10:44:50 AM Subject: [c-prog] Segmentation fault in a simple function. I wrote the following code to trim a char string at left. The code is simple and I think there is no error, but when I run, I get a "Segmentation fault". If I use the debugger (gdb), the error does not occur. May anyone help me to understand why? and where is the error? Thanks a lot. I am using a Red Hat distribution, version 7.3 and a cc release, version 2.96. == code start (ltrim.c) == #include <errno.h> #include <string.h> /* ltrim() function * parameters: * string = the string to trim at left * return value: * char* : the trimmed string at left */ char* ltrim(char *strn ){ if (strn!=NULL) { char *p=strn; char *tmp_buffer = NULL; int l = strlen(p); printf( "ltrim() : [%p] [%s], len: %d \n", p, p, l); tmp_buffer = (char*) calloc( l+1, sizeof(char) ); if (tmp_buffer!=NULL) { int x=0; char c='\0'; printf("buffer before the filling: [%p] [%s] [%d]\n", tmp_buffer, tmp_buffer, strlen(tmp_buffer) ); strcpy( tmp_buffer, p ); printf("buffer after the filling with the string: [%p] [%s] [%d] \n", tmp_buffer, tmp_buffer, strlen(tmp_buffer) ); while ( (c=tmp_buffer[x++])!=0 && c==' ' ) { printf("p: %d, c: [%c] 0X%2X\n",x, c, c); } printf( "reset string [%p], len: %d to 0\n", p, l ); memset((void*)p, 0, l ); printf("prepare the return parameter\n"); strcpy(p, &tmp_buffer[x-1]); printf( "new string: [%s] l: %d -> free memory : [%p] [%s] [%d] \n", p, strlen(p), tmp_buffer, tmp_buffer, strlen(tmp_buffer) ); free(tmp_buffer); tmp_buffer = NULL; printf( "memory freed\n"); printf( "ltrim() : (trimmed) [%s], len: %d \n", strn, strlen (strn) );printf( "ltrim() : (trimmed) [%s], len: %d \n", strn, strlen (strn) ); } else { printf( "ltrim() : cannot allocate memory [%s] \n", strerror (errno)); } } printf("return the value\n"); return (char*) strn; } int main (char **argz, char argc) { char *s0 = " bye 1 "; char *d0 = ltrim(s0); printf( "before: [%s] after [%s] \n", s0, d0 ); exit(0); } == code end ==== To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>. Yahoo! Groups Links ____________________________________________________________________________________ The fish are biting. Get more visitors on your site using Yahoo! Search Marketing. http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>. Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/c-prog/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/c-prog/join (Yahoo! ID required) <*> To change settings via email: mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
