473,386 Members | 1,705 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,386 software developers and data experts.

Using strcat

How can I use strcat in the following scenario?
char line[250] has the following content "(360+c)" and not declared as
string literal

The purpose is that I want to prepend '0-' in front of line, for my formula
to work. I tried to do the following, but prev still contains the original
"(360+c)" and final gives me garbage characters

char prev[1] = '0';
strcpy(var, line);
strcat(prev, var);
strcpy(final, prev);

line --> "(360+c)"
prev should be --> "0-(360+c)"
final should be --> "0-(360+c)"

what is wrong with the char prev[1] = '0'; ?

Thank you for the inputs
Nov 13 '05 #1
10 5290
Nicholas wrote:
How can I use strcat in the following scenario?
char line[250] has the following content "(360+c)" and not declared as
string literal

The purpose is that I want to prepend '0-' in front of line, for my formula
to work. I tried to do the following, but prev still contains the original
"(360+c)" and final gives me garbage characters

char prev[1] = '0';
strcpy(var, line);
strcat(prev, var);
strcpy(final, prev);

line --> "(360+c)"
prev should be --> "0-(360+c)"
final should be --> "0-(360+c)"

what is wrong with the char prev[1] = '0'; ?

Thank you for the inputs


Space. It's all a matter of space.
The variable "prev" is only one character. No more, no less.
You are trying to add more characters to a position that
holds only one character.

Consider a bookshelf. A book shelf can only hold a finite
amount of books. What happens when you add more books to
a full shelf? We call it "undefined behavior" around here.
{See also (search for) Nasal Demons}

Your destination needs to have room to hold all of the
characters plus one for the terminating null.
char final[256]; // Lots of space.
const char prepend_text[] = "0-";

if (strlen(line) + strlen(prepend_text) + 1 < 256)
{
strcpy(final, "0-");
strcat(final, line);
}
else
{
Big_Problems_In_Little_China("Kurt Russell");
}

Also, you can eliminate some operations by copying everything
int "final" rather than using temporaries.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #2
Nicholas wrote:
How can I use strcat in the following scenario?
char line[250] has the following content "(360+c)" and not declared as
string literal

The purpose is that I want to prepend '0-' in front of line,
char line[250] = "(360+c)", final[252] = "0-";
strcat(final, line);
what is wrong with the char prev[1] = '0'; ?


1. It is not legal to assign a char to an array.
2. It is not a string. A string must contain a '\0' at the end.

char prev[] = "0"; produces a string consisting of 2 chars: '0' and '\0'.

Jirka

Nov 13 '05 #3
On Tue, 09 Sep 2003 15:42:20 GMT, "Nicholas" <ni**@yahoo.com> wrote:
The purpose is that I want to prepend '0-' in front of line, for my formula
to work. I tried to do the following, but prev still contains the original
"(360+c)" and final gives me garbage characters

char prev[1] = '0';
strcpy(var, line);
strcat(prev, var);
strcpy(final, prev);
what is wrong with the char prev[1] = '0'; ?


You're only allocating one character - that's not a string and can't be
used by string functions. Then you add the contents of var (why did you
bother copying from line?) to memory that has not been allocated for prev,
so you're clobbering your stack. Also, where's final's definition?

char *prev = "0-";
strcpy( final, prev );
strcat( final, line );
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #4

"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:xq***************@newssvr33.news.prodigy.com. ..
Nicholas wrote:
How can I use strcat in the following scenario?
char line[250] has the following content "(360+c)" and not declared as
string literal

The purpose is that I want to prepend '0-' in front of line, for my formula to work. I tried to do the following, but prev still contains the original "(360+c)" and final gives me garbage characters

char prev[1] = '0';
strcpy(var, line);
strcat(prev, var);
strcpy(final, prev);

line --> "(360+c)"
prev should be --> "0-(360+c)"
final should be --> "0-(360+c)"

what is wrong with the char prev[1] = '0'; ?

Thank you for the inputs


Space. It's all a matter of space.
The variable "prev" is only one character. No more, no less.
You are trying to add more characters to a position that
holds only one character.

Consider a bookshelf. A book shelf can only hold a finite
amount of books. What happens when you add more books to
a full shelf? We call it "undefined behavior" around here.
{See also (search for) Nasal Demons}

Your destination needs to have room to hold all of the
characters plus one for the terminating null.
char final[256]; // Lots of space.
const char prepend_text[] = "0-";

if (strlen(line) + strlen(prepend_text) + 1 < 256)
{
strcpy(final, "0-");
strcat(final, line);
}
else
{
Big_Problems_In_Little_China("Kurt Russell");
}

Also, you can eliminate some operations by copying everything
int "final" rather than using temporaries.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Hi Thomas. Thank you for the explanation. :) It has been very helpful, thank
you once again.

Nov 13 '05 #5
On Tue, 09 Sep 2003 15:42:20 GMT, "Nicholas" <ni**@yahoo.com> wrote:
How can I use strcat in the following scenario?
char line[250] has the following content "(360+c)" and not declared as
string literal

The purpose is that I want to prepend '0-' in front of line, for my formula
to work. I tried to do the following, but prev still contains the original
"(360+c)" and final gives me garbage characters

char prev[1] = '0';


This is not allowed for the same reason that
int foo[10] = 1, 2, 3;
is also not allowed.

This type of initialization requires a compound initializer,
so should be written as:
char prev[1] = { '0' };
int foo[10] = { 1, 2, 3 };

However defining an array with only one element is somewhat
unusual. If you can accept a larger array then you could
also initialize it with one of the following methods:

char prev[2] = "0";
char prev[2] = { "0" };

Nick.
Nov 13 '05 #6
Jirka Klaue <jk****@ee.tu-berlin.de> writes:
Nicholas wrote:

[...]
what is wrong with the char prev[1] = '0'; ?


1. It is not legal to assign a char to an array.
2. It is not a string. A string must contain a '\0' at the end.

char prev[] = "0"; produces a string consisting of 2 chars: '0' and '\0'.


At least one compiler only gives a warning for
char prev[1] = '0';

The warning I got was:
"tmp.c", line 5: warning: {}-enclosed initializer required

Apparently the compiler, in effect, implicitly supplies the missing
braces, making the declaration look like
char prev[1] = { '0' };

I'm not convinced that this attempt to be helpful is entirely
successful, especially since adding braces doesn't fix the actual
problem.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #7
Keith Thompson wrote:
Jirka Klaue <jk****@ee.tu-berlin.de> writes:
Nicholas wrote:
what is wrong with the char prev[1] = '0'; ?


1. It is not legal to assign a char to an array.
2. It is not a string. A string must contain a '\0' at the end.

char prev[] = "0"; produces a string consisting of 2 chars: '0' and '\0'.


At least one compiler only gives a warning for
char prev[1] = '0';


It has to. A diagnostic is required.

6.7.8 #14
An array of character type may be initialized by a character string literal,
optionally enclosed in braces. Successive characters of the character string
literal (including the terminating null character if there is room or if the
array is of unknown size) initialize the elements of the array.
Apparently the compiler, in effect, implicitly supplies the missing
braces, making the declaration look like
char prev[1] = { '0' };
So what?
I'm not convinced that this attempt to be helpful is entirely
successful, especially since adding braces doesn't fix the actual
problem.


Your attempt or mine? ;-)

I said, that char prev[1] = '0' is wrong and even if it would "work",
it wouldn't be a string. Then I said that char prev[] = "0" is probably
what the OP wanted.

You said, that you get a warning for char prev[1] = '0' and that adding
braces wouldn't help.

How does this contradict what I said?

Jirka

Nov 13 '05 #8
In <lz************@cts.com> Keith Thompson <ks*@cts.com> writes:
Jirka Klaue <jk****@ee.tu-berlin.de> writes:
Nicholas wrote:[...]
> what is wrong with the char prev[1] = '0'; ?


1. It is not legal to assign a char to an array.
2. It is not a string. A string must contain a '\0' at the end.

char prev[] = "0"; produces a string consisting of 2 chars: '0' and '\0'.


At least one compiler only gives a warning for
char prev[1] = '0';

The warning I got was:
"tmp.c", line 5: warning: {}-enclosed initializer required


Isn't it enough?
Apparently the compiler, in effect, implicitly supplies the missing
braces, making the declaration look like
char prev[1] = { '0' };

I'm not convinced that this attempt to be helpful is entirely
successful, especially since adding braces doesn't fix the actual
problem.


The helpful bit was the diagnostic. Whoever ignores it gets exactly what
he deserves.

The VAX C compiler I used back when I was a newbie was quite good at
detecting missing semicolons and adding them to the translation unit
(after issuing a warning). I've never attempted to use the result of
any compilation that generated the SEMICOLONADDED warning.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #9
Jirka Klaue <jk****@ee.tu-berlin.de> writes:
Keith Thompson wrote:
Jirka Klaue <jk****@ee.tu-berlin.de> writes:
Nicholas wrote:
what is wrong with the char prev[1] = '0'; ?

1. It is not legal to assign a char to an array.
2. It is not a string. A string must contain a '\0' at the end.

char prev[] = "0"; produces a string consisting of 2 chars: '0' and '\0'.

At least one compiler only gives a warning for
char prev[1] = '0';


It has to. A diagnostic is required.

6.7.8 #14
An array of character type may be initialized by a character
string literal, optionally enclosed in braces. Successive
characters of the character string literal (including the
terminating null character if there is room or if the array is of
unknown size) initialize the elements of the array.
Apparently the compiler, in effect, implicitly supplies the missing
braces, making the declaration look like
char prev[1] = { '0' };


So what?
I'm not convinced that this attempt to be helpful is entirely
successful, especially since adding braces doesn't fix the actual
problem.


Your attempt or mine? ;-)


The compiler's.

Apparently I was unclear.

The warning given by the compiler in question is perfectly legal as
far as the standard is concerned; the standard only requires a
diagnostic, and the warning message clearly qualifies.

What I was complaining about was the compiler's poor attempt to guess
what the programmer really meant. By issuing a warning rather than an
error, it implies that leaving off the braces is only a minor issue
(far too many programmers ignore warnings). The compiler apparently
then proceeded as if the braces had been supplied; adding the braces
would make the program legal, but would not correct the actual
problem.

(The compiler in question, BTW, is Sun's.)
I said, that char prev[1] = '0' is wrong and even if it would "work",
it wouldn't be a string. Then I said that char prev[] = "0" is probably
what the OP wanted.
Agreed.
You said, that you get a warning for char prev[1] = '0' and that adding
braces wouldn't help.

How does this contradict what I said?


It doesn't, and I didn't mean to imply that it does.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #10
Keith Thompson wrote:
Nicholas wrote: ....>what is wrong with the char prev[1] = '0'; ? ....At least one compiler only gives a warning for
char prev[1] = '0';
.... (The compiler in question, BTW, is Sun's.)


Intel's compiler issues only a warning, too. Microsoft's and GCC treat
this as an error.

Jirka

Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Patrick Coleman | last post by:
Hi, I have the following code: char request = "GET "; strcat(request, path); //Path is the path section of a url ie. "/path/test.htm" strcat(request, " HTTP/1.1"); cout<<request<<"\n";...
44
by: Nicolas | last post by:
On most implementations of the standard C library, the functions that copy characters in a previously allocated buffer, like strcpy or strcat, are returning the pointer to that buffer. On my...
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...
1
by: rw | last post by:
I have a Microsoft C/C++ compiler version 12.00.8168 installed on my PC running Windows 2000. I have no problem to use the MS C/C++ compiler to compile the following 'C' program and get an...
9
by: cicalese | last post by:
For starters, I am a C novice. I don't fully understand how strings work. I am running on a linux box. I compile w/ gcc. I have a string called 'myabqcommand' that is being overwritten when i use...
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...
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.