Thread: cygwin -> unix , my code not working properly ;(

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    25

    cygwin -> unix , my code not working properly ;(

    Hi,

    I recently make this code to concate 2 string...i am using cygwin at home...and this code working as i expected :
    Code:
    char *str_concat( char *string1, char *string2 )
    {
    		
    	char *string3;
    	char *concated_string;
    		
    	int len1, len2;
                
                   /* edited */	
    
    	string3 = '\0';
    
    	return concated_string;
    
    }
    so when i put for example str_concat( mike, mike ) it will return "mikemike" (without the quote)

    however when try to run it at school, in unix machine it doesn't work ( I've change the file format using dos2unix ). if i put str_concat( mike, mike ) it will return "mikemikelv55/k" (without quote) instead of "mikemike" -> and somethin weird, it not always giving addition "lv55/k" sometimes it gives like "xxlv55/k/x" in the back...always in the back...

    this is mess up the whole program of mine ;( it is due on monday..and only becoz of one small part not working, the whole program not gonna work )

    pls help

    Ferdinand
    Last edited by CyC|OpS; 05-18-2002 at 07:25 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > string3 = '\0';

    Should be
    *string3 = '\0';

    Oh, and you don't (or perhaps shouldn't) cast the result of malloc.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    Thanks Salem!!!

    It did workingggg, omg i dont believe it...

    thank you very much

    o yeah about the malloc... do u mean that I dont need to allocated the memory?


    sincerely,

    ferdinand

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > do u mean that I dont need to allocated the memory
    Yes you DO need to allocate the memory

    But it's better to write this in ANSI-C
    string3 = malloc( len1 + len2 + 1 );

    If you've included stdlib.h, then the normal C rules for converting void* into the appropriate type will take over for you

    If you haven't included stdlib.h, you will get a warning (a warning which the cast will suppress)

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    oh okie...
    i dont know about that =)

    I think thread closed now.
    Last edited by CyC|OpS; 05-18-2002 at 05:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to properly call an executable from C code?
    By remy06 in forum C Programming
    Replies: 3
    Last Post: 05-14-2009, 03:48 AM
  2. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  3. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  4. Working Code Samples Wanted
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 10:05 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM