By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

strcat syntax with int and char

P: n/a
Hi

I have

char String[] = "Time";
char temp[];
int slot;

slot = 1;

What I need is:= String[] = "Time1"

temp = (char)slot
strcat(String, temp);

Something is a miss - could someone fix it for me pls.

Nov 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


On Nov 5, 8:19 pm, "2005" <uws2...@yahoo.comwrote:
Hi

I have

char String[] = "Time";
char temp[];
int slot;

slot = 1;

What I need is:= String[] = "Time1"

temp = (char)slot
strcat(String, temp);

Something is a miss - could someone fix it for me pls.
Java programmer?

something like the following:

const char *String = "Time";
char *temp = (char*)malloc( strlen( String ) + 20 +1 ); /*assume int
can be printed in 20 chars */
int slot = 1;

sprintf( temp, "%s%d", String, slot );
/* code using temp */
free( temp);

/* This is off the top of my head - I haven't compiled this - brave in
this group */

Nov 5 '06 #2

P: n/a
gep
2005 napsal:
Hi

I have

char String[] = "Time";
char temp[];
int slot;

slot = 1;

What I need is:= String[] = "Time1"

temp = (char)slot
strcat(String, temp);

Something is a miss - could someone fix it for me pls.
You want something like this:

char String[LONG_ENOUGH];
sprintf(String, "Time%i", slot)

or just
char String[] = "TimeX"; /* the X is important */
String[5] = '0' + (char)slot;

but it works only for 0 <= slot <= 9.

--js

Nov 5 '06 #3

P: n/a
That should be
>
const char *String = "Time";
char *temp = (char*)malloc( strlen( String ) + 20 +1 ); /*assume int
can be printed in 20 chars */
int slot = 1;
if ( temp != NULL ) /*need to test that malloc succeeded*/
{
>
sprintf( temp, "%s%d", String, slot );
/* code using temp */
free( temp);
}

Nov 6 '06 #4

P: n/a
gep wrote:
char String[] = "TimeX"; /* the X is important */
String[5] = '0' + (char)slot;

but it works only for 0 <= slot <= 9.
I think you mean:
String[4] = '0' + slot;

Arrays are indexed from 0 in C, and also it does not make any
difference if you cast 'slot'.

Nov 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.