473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

strcat syntax with int and char

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
4 13011


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
5
by: Kevin C. | last post by:
Never mind my last post about pointer subtraction, I traced the code and found the offender to be strcat. char call = "del "; system(strcat(strcat(call, del->table_name), ".tab")); After this...
8
by: ctara_shafa | last post by:
Hi, I have a following problem: I'm creating a list and one of the fields should contain the date. Firstly I ask the user for the year, month and day and then I'd like to collect all this data in...
3
by: aldonnelley | last post by:
Hi there. I'm just learning c++, and this is driving me nuts. I'm trying to save image files generated in a for loop with a filename built using strcat with: - a char base file name + a...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
7
by: 2005 | last post by:
Hi I have char String = "Time"; char temp; int slot; slot = 1;
4
by: nick048 | last post by:
Hi, I have this problem: int n; char nToChar; char firstString = "The number is: "; char resp; /* HERE MAIN WITH THE INPUT OF n*/
3
by: sail0r | last post by:
Perhaps this is obvious but I am not sure what is going on... Here is the relevant code: char *command; char *argument; char url="file:///usr/u/myname/Project/cats/"; char...
1
by: intrealm | last post by:
Hi there, I am having a bit of an issue when trying to concatenate strings using char * pointers. Here is what I need: I need to add a list of ID's to a string that is a SQL statement so...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.