473,795 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13040


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
6178
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 strcat (mystract). Program below. However this appears not to have fixed the problem and I don't know why it shouldn't ? Any further help as to what else I am doing wrong will be appreciated regards
5
5575
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 line, trying to dereference del results in page faults or garbage. This makes me wonder how strcat is implemented, in terms of what it actually does with the arguments. Seems like it actually moves the pointer. Anyone know?
8
539
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 one field. To do this I tried to concatenate those 3 numbers into one using strcat. A piece of code is as follows /*function to enter the date*/ char * date (void) {
3
2952
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 character to identify the individual files that is indexed from a char array by the int index of the for loop + a char file extension.
87
5170
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 the library, and other resources related to managed strings are available for download from the CERT web site at: http://www.cert.org/secure-coding/managedstring.html
7
482
by: 2005 | last post by:
Hi I have char String = "Time"; char temp; int slot; slot = 1;
4
4710
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
1762
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 target_path="/tmp/abc"; command=strtok(buf,":\n\r"); argument=strtok(NULL,"\n\r");
1
4430
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 that I can end up with: SELECT DISTINCT Object_ID FROM Object_Interactions WHERE Object_ID IN ('UUID1', 'UUID2') ORDER BY Object_ID ASC; Here is what I am doing:
28
3908
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
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.