Using strcat

N

Nicholas

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
 
T

Thomas Matthews

Nicholas said:
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
 
J

Jirka Klaue

Nicholas said:
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
 
K

Kevin D. Quitt

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 );
 
N

Nicholas

Thomas Matthews said:
Nicholas said:
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.
 
N

Nick Austin

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.
 
K

Keith Thompson

Jirka Klaue said:
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.
 
J

Jirka Klaue

Keith said:
Jirka Klaue said:
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
 
D

Dan Pop

In said:
Jirka Klaue said:
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
 
K

Keith Thompson

Jirka Klaue said:
Keith said:
Jirka Klaue said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top