Hi ,
I have a string array having a bunch of values. I need to sort this array, remove duplicate values from this array and reset the filtered array (without duplicate values) to a new array.
I am new to C programming. I have written some code, but it fails with a core dump always. Can some one please correct my code or send me an easy way to do this.
Thanks.
Code:code snippet: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> main () { int stat = 0; int ctr = 4; int inx = 0; int status = 0; int j = 0; int k = 0; int reset_count = 0; char **lov_reset_values = 0; char *str[] = { "test", "test1", "test2", "test" }; for ( inx = 0; inx < ctr; inx ++ ) { printf ( "str[%d] = %s\n", inx, str[inx] ); } for(j=0;j<ctr;j++) { status = 0; for(k=1;k<=ctr;k++) { printf ( "entered after k= 0\n" ); if( strcmp (str[j],str[k] ) == 0 ) { printf ( "matched\n" ); status = 1; break; } } if ( status == 0 ) { if (reset_count == 0 ) { printf ( "reset_count = 0\n" ); lov_reset_values = (char **)malloc( sizeof(char *)); } else { printf ( "reset_count = %d\n", reset_count ); lov_reset_values = (char **)realloc( lov_reset_values, sizeof(char *)*(reset_count+1) ); } lov_reset_values[reset_count] = (char *)malloc( sizeof( char )*(strlen(str[j])+1)); strcpy ( lov_reset_values[reset_count], (str)[j] ); printf ( "lov_reset_values[%d] = %s\n", reset_count, lov_reset_values[reset_count]); reset_count = reset_count + 1; } } printf (" reset_count = %d\n", reset_count ); /* for ( inx = 0; inx < reset_count; inx++ ) { printf ( "lov_reset_values[%d] = %s\n", inx, lov_reset_values[inx] ); } */ return stat; }



LinkBack URL
About LinkBacks


