C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-18-2002, 02:27 AM   #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.
CyC|OpS is offline   Reply With Quote
Old 05-18-2002, 03:21 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> string3 = '\0';

Should be
*string3 = '\0';

Oh, and you don't (or perhaps shouldn't) cast the result of malloc.
Salem is offline   Reply With Quote
Old 05-18-2002, 03:37 AM   #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
CyC|OpS is offline   Reply With Quote
Old 05-18-2002, 04:00 AM   #4
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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)
Salem is offline   Reply With Quote
Old 05-18-2002, 04:08 AM   #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.
CyC|OpS is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to properly call an executable from C code? remy06 C Programming 3 05-14-2009 03:48 AM
Who will map the scan code (inserted by VKD_Force_keys) to virtual key code? Unregistered Windows Programming 0 02-21-2002 06:05 PM
opengl code not working Unregistered Windows Programming 4 02-14-2002 10:01 PM
Working Code Samples Wanted Unregistered C Programming 2 02-13-2002 10:05 PM
Linked List Working Code Linette C++ Programming 9 01-24-2002 12:00 PM


All times are GMT -6. The time now is 06:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22