GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Apr-2004, 02:49
jack jack is offline
New Member
 
Join Date: Jan 2004
Posts: 15
jack is on a distinguished road
Unhappy

urgent help:string concat


hi below there is a simple concatenation program. but i'm getting segmentation fault in it. the problem is that i'm tring to concatenate an integer value to the string but in vain. please help me.
thnx

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
	char *s1,*s2, *s3, *all, *result, *thecode;
int x=567;
	s1 = "Hello ";
	s2 = "world! ";
	s3 = (char *)x;

	all= (char *)calloc(strlen(s1) + strlen(s2) + strlen(s3), sizeof(char));

	strcat (all,s1);
	strcat (all,s2);
	strcat (all,s3);


	printf ("The string is = %s\n",all);
}
Last edited by dsmith : 11-Apr-2004 at 07:21. Reason: Use [c] & [/c] for syntax highlighting.
  #2  
Old 11-Apr-2004, 07:28
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello Jack. I don't think that strcat is your problem as much as this:
CPP / C++ / C Code:
s3 = (char *)x;
This doesn't do what you think it does. You can not typecast a string from an integer. This is going to set the address of s3 to memory location 576.

Also, on strcat, you probably wouldn't get the result you wanted either, because it is going to entirely overwrite the string. I have modified your code to get the results you wanted:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
	char *s1,*s2, *s3, *all, *result, *thecode;
int x=567;
	s1 = "Hello ";
	s2 = "world! ";
	s3 = itoa(x);

	all= (char *)calloc(strlen(s1) + strlen(s2) + strlen(s3), sizeof(char));

	strcat (all,s1);
	strcpy (all,s2);
	strcpy(all,s3);


	printf ("The string is = %s\n",all);
}

Unfortunately, most compilers don't have itoa (which is the opposite of atoi). If you are so unfortunate, here is a routine that I wrote quite some time ago to convert numbers to a char*
CPP / C++ / C Code:
char *numasc(int num)
{
	char	string[30],
			*ret;
	int		digit,
			left,
			pos,
			negative = 0;


	if(num == 0){
		ret = new char[2];
		*ret = '0';
		*(ret+1) = 0;
		return ret;
  	}
	string[29] = 0;
	left = num;
	pos = 28;
	if(left < 0){
		left = abs(left);
		negative = 1;
	}

	while((left) && (pos)){
		digit = left % 10;
		left = (int) left/10;

		string[pos] = digit+48;
		pos--;
	}
	if(negative){
		string[pos] = '-';
		pos--;
  	}
    ret = new char[30-pos];
	strcpy(ret,&string[pos+1]);

	return(ret);
}

Hope this helps,
d
  #3  
Old 11-Apr-2004, 08:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
D, the problems I noticed with your routine is that the string[] array is a local buffer that goes out of scope as soon as the routine returns, therefore the data technically does not exist upon return.

Instead you can use sprintf() to convert the number to a string:
CPP / C++ / C Code:
  char *s1,*s2, s3[10], *all, *result, *thecode;  // You must allocate space for s3
  int x=567;
  s1 = "Hello ";
  s2 = "world! ";
  sprintf(s3, "%d", x);
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #4  
Old 11-Apr-2004, 09:03
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by WaltP
D, the problems I noticed with your routine is that the string[] array is a local buffer that goes out of scope as soon as the routine returns, therefore the data technically does not exist upon return.

You are absolutely correct. Like I said, I wrote this a while ago and have always wanted to fix it to be more efficient.

I keep forgetting about sprintf. You could avoid strcat all together with sprintf.
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  char *s1,*s2,*all, *result, *thecode;
int x=567;
  s1 = "Hello";
  s2 = "world!";

  all= (char *)calloc(strlen(s1) + strlen(s2) + 10, sizeof(char));

  sprintf(all,"%s %s %d",s1,s2,x);
  printf ("The string is = %s\n",all);
}
I like this much better. You can do all of your formatting with sprintf and get a single string output. Notice that I removed the spaces from s1 and s2 and included them in the sprintf statement.

Thanks Walt.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent ! Pls Help Me ! mycashmoney C Programming Language 4 01-Jul-2006 22:49
urgent help:hash function jack C Programming Language 5 05-Apr-2004 12:18
urgent: problem + output filters jack Apache Web Server Forum 0 04-Feb-2004 22:32
urgent needs :apache + random function jack Apache Web Server Forum 0 19-Jan-2004 17:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:21.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.