Thread: Pointer Problems

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    Pointer Problems

    Hi all. So the problem im having seems apparently to be pointer related. Heres the source code.
    Code:
    /* squeeze: modified to remove any matching chars from string2 to string1 */#include #include #include void squeeze(char * s1 , char * s2 );int main(int argc, char **argv){	if( argc != 3 )		printf( "Usage: %s S1 S2, where any characters in S1 that are in S2 are deleted\n" , argv[ 0 ] );	else{		squeeze( argv[ 1 ] , argv[ 2 ] );	}	return 0;}void squeeze(char * s1 , char * s2 ){	int i , j , found = 0;	char * new_string = ( char * ) malloc( sizeof( char ) * strlen( s1 ) );		for( i = 0; i < strlen( s1 ); ++i){		for( j = 0; j < strlen( s2 ); ++j){			if( s1[ i ] == s2[ j ] )				++found;		}		if( !( found ) )			new_string[ i ] = s1[ i ];		found = 0;	}	printf( "%s" , new_string );}
    Thanks for any help in advance!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I am not reading that until you format it properly.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    sorry about the formatting of the source. its not letting me edit it at the moment.

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Code:
    /* squeeze: modified to remove any matching chars from string2 to string1 */#include #include #include void squeeze(char * s1 , char * s2 );int main(int argc, char **argv){	if( argc != 3 )		printf( "Usage: %s S1 S2, where any characters in S1 that are in S2 are deleted\n" , argv[ 0 ] );	else{		squeeze( argv[ 1 ] , argv[ 2 ] );	}	return 0;}void squeeze(char * s1 , char * s2 ){	int i , j , found = 0;	char * new_string = ( char * ) malloc( sizeof( char ) * strlen( s1 ) );		for( i = 0; i < strlen( s1 ); ++i){		for( j = 0; j < strlen( s2 ); ++j){			if( s1[ i ] == s2[ j ] )				++found;		}		if( !( found ) )			new_string[ i ] = s1[ i ];		found = 0;	}	printf( "%s" , new_string );}

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    wow that is not appearing as i post it. im not sure what to do.

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    i attached the source as a file. not sure what else to do. thanks if your willing to download and help!!
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    /* squeeze: modified to remove any matching chars from string2 to string1 */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void squeeze(char * s1 , char * s2 );
    int main(int argc, char **argv)
    {
    	if( argc != 3 )
    		printf( "Usage: %s S1 S2, where any characters in S1 that are in S2 are deleted\n" , argv[ 0 ] );
    	else{
    		squeeze( argv[ 1 ] , argv[ 2 ] );
    	}
    	return 0;
    }
    void squeeze(char * s1 , char * s2 ){
    	int i , j , found = 0;
    	char * new_string = ( char * ) malloc( sizeof( char ) * strlen( s1 ) );
    	
    	for( i = 0; i < strlen( s1 ); ++i){
    		for( j = 0; j < strlen( s2 ); ++j){
    			if( s1[ i ] == s2[ j ] )
    				++found;
    		}
    		if( !( found ) )
    			new_string[ i ] = s1[ i ];
    		found = 0;
    	}
    	printf( "%s" , new_string );
    }
    You need to run this through your debugger. What happens if string 1 contains multiple characters that are in string 2?

    You also need to make sure your new_string is properly terminated. And you should not be casting the return value of your malloc call, and you don't need the size_of(char).

    Jim

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by jwroblewski44 View Post
    i attached the source as a file. not sure what else to do. thanks if your willing to download and help!!
    The problem lies with the offset of array new_string[].
    Its index needs to be separate from that of s1[] or s2[].
    Code:
    new_string[ i ] = s1[ i ];

  9. #9
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    haha thanks everyone. @jim i tried to debug, but codelite's debugger interface was, well, buggy . im still a little hesitant on learning the cli gdb, im sure will take a lot of determination. about forgot about the null term issue thanks jim.... although im not to sure about what problem your seeing "What happens if string 1 contains multiple characters that are in string 2?"...... if any char in string1 is in string2, it wont be added. every char in string1 should checked.

    and after scratching my head contemplating that the new_string needs its own offset tracker.... working on v 1.1 -- thanks itCbitC

  10. #10
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Code:
    /* squeeze: modified to remove any matching chars of string2 from string1 */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    char * squeeze(char * s1 , char * s2 );
    int main(int argc, char **argv)
    {
        if( argc != 3 )
            printf( "Usage: %s S1 S2, where any characters in S1 that are in S2 are deleted\n" , argv[ 0 ] );
        else
            printf( "%s\n" , squeeze( argv[ 1 ] , argv[ 2 ] ) );
        return 0;
    }
    char * squeeze(char * s1 , char * s2 ){
        int i , j , k = 0 , found = 0;
        char * new_string = malloc( strlen( s1 + 1 ) );
         
        for( i = 0; i < strlen( s1 ); ++i){
            for( j = 0; j < strlen( s2 ); ++j){
                if( s1[ i ] == s2[ j ] )
                    ++found;
            }
            if( !( found ) )
                new_string[ k++ ] = s1[ i ];
            found = 0;
        }
        new_string[ k ] = '\0';
        return new_string;
    }
    heres the new code. does anybody see any issues? please let me know!

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    char * new_string = malloc( strlen( s1 + 1 ) );
    This will allocate two bytes too little.
    Correct:
    Code:
    char *new_string = malloc(strlen(s1) + 1);
    Bye, Andreas

  12. #12
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    ahh yes. Thank you very much!

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by jwroblewski44 View Post
    Code:
    if ( s1[ i ] == s2[ j ] )
       ++found;
    heres the new code. does anybody see any issues? please let me know!
    No use iterating over s2 once a match is found. Instead terminate the search and move onto the next character in s1. So the above piece of code can be written more efficiently as:
    Code:
    if (s1[i] == s2[j]) {
        ++found;
        break;
    }

  14. #14
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    indeed it would, and i will update it immediately.

    postscript: also realized i didnt free my dynamic string, slightly suprised no one has caught my blunder yet!

    Code:
    /* squeeze: modified to remove any matching chars of string2 from string1 */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    char * squeeze(char * s1 , char * s2 );
    int main(int argc, char **argv)
    {
        if( argc != 3 )
            printf( "Usage: %s S1 S2, where any characters in S1 that are in S2 are deleted\n" , argv[ 0 ] );
        else
            char * new_string = squeeze( argv[ 1 ] , argv[ 2 ] );
            printf( "%s\n" , new_string );
            free( new_string );
        return 0;
    }
    char * squeeze(char * s1 , char * s2 ){
        int i , j , k = 0 , found = 0;
        char * new_string = malloc( strlen( ( s1 ) + 1 ) );
         
        for( i = 0; i < strlen( s1 ); ++i){
            for( j = 0; j < strlen( s2 ); ++j){
                if( s1[ i ] == s2[ j ] ){
                    ++found;
                    break;
                }
            }
            if( !( found ) )
                new_string[ k++ ] = s1[ i ];
            found = 0;
        }
        new_string[ k ] = '\0';
        return new_string;
    }
    ....thanks for scanning and showing my errors! Any more...?

  15. #15
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    one question though, does inserting that
    Code:
    break
    into the bracketed off
    Code:
    if
    statement cause it to exit the
    Code:
    for
    loop when a match is found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Problems
    By TieFighter in forum C Programming
    Replies: 6
    Last Post: 02-18-2010, 05:36 PM
  2. More pointer problems :(
    By mike_g in forum C Programming
    Replies: 9
    Last Post: 07-21-2008, 07:28 AM
  3. pointer problems
    By vidioholic in forum C Programming
    Replies: 44
    Last Post: 10-12-2007, 07:22 PM
  4. Pointer problems :(
    By ryptide in forum C Programming
    Replies: 7
    Last Post: 06-16-2004, 03:37 AM
  5. pointer problems
    By tetraflare in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2002, 09:01 PM