Thread: easy problem with strchr-clone

  1. #1
    Unregistered
    Guest

    easy problem with strchr-clone

    Hi,
    i have some questions/problems with the following snippet:

    Code:
    char *strchr( const char *str, int c )
    {
    	char *buf = ( char* )malloc( strlen( str ) );
    	while( *str )
    	{
    		if( *str == c )
    		{
    			while( *str )
    				*buf++ = *str++;
    			*buf = 0;
    			return buf;
    		}
    	*str++;
    	}
    	return NULL;
    }
    Why will

    Code:
        char *text = "hello world!";
        char *new = strchr( text, 'w' );
    ...return NULL? And why will

    Code:
        char *text = "hello world!";
        char *new = strchr( text, CHAR_THAT_DOESNT_OCCUR_IN_STRING );
    ...crash??
    Please help me... (with the problem/mistake, no style flames >:( )
    Thanks in advance,
    bye.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mostly, you're returning the wrong thing - a modified buf

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *my_strchr( const char *str, int c )
    {
        while ( *str )
        {
            if ( *str == c )
            {
                // only malloc if we actually copy something
                char *buf = malloc( strlen( str ) + 1 );    // forgot the +1
                char *temp = buf;                           // buf is returned, dont change it
                while ( *str )
                    *temp++ = *str++;
                *temp = 0;
                return buf;
            }
            str++;
        }
        return NULL;
    }
    
    
    int main ( ) {
        char *text = "hello world!";
        char *new;
        
        new = my_strchr( text, 'w' );
        printf( "%p %s\n", new, new ? new : "(null)" );
        new = my_strchr( text, 'z' );
        printf( "%p %s\n", new, new ? new : "(null)" );
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    44

    strchr does not need to allocate any memory

    strchr, as defined by ANSI, should only return the pointer to the first occurance of the character in the string and not the position of that character in a newly allocated string.

    You don't need to use any additional memory for the string: trying to do simply makes the problem more difficult than it needs to be.

    Try again, but this time return a pointer within the same string as the one passed by the caller! Good luck!

    Ian Woods

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very easy C question(s), pls help
    By humanturk in forum C Programming
    Replies: 4
    Last Post: 06-17-2009, 06:13 PM
  2. Easy problem with while!
    By AdampqmabA in forum C++ Programming
    Replies: 6
    Last Post: 10-05-2008, 04:45 PM
  3. is it possible to pass arguments to the clone fn function?
    By smoking81 in forum Linux Programming
    Replies: 4
    Last Post: 09-12-2008, 10:27 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. Easy Peasy Function Problem
    By The Gweech in forum C++ Programming
    Replies: 10
    Last Post: 08-31-2001, 06:51 PM