strcpy and strcat problem

J

JC

hi,

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {
char a[2]="ab";
char b[2]="cd";
char c[4]=" \0";
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding? anything wrong?
please help!!.

Thanks

JC

ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
abcd"
 
M

Mike Wahler

JC said:
hi,

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {

int main() {
char a[2]="ab";

char a[] = "ab";
char b[2]="cd";

char b[] = "cd";
char c[4]=" \0";

char c[sizeof a + sizeof b + 1] = {0};
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding?

Your arrays did not provide room for the string terminators.
anything wrong?

Yes. Undefined behavior.

-Mike
please help!!.

Thanks

JC

ps. if i use strcpy(c,"ab");

OK so far. The literal contains a terminator.
and strcat(c,"cd"); i can get the result.."c is

Not OK, the terminator from the literal "cd" will
be written to c[4] -- out of bounds -- undefined
behavior.

Only by accident.

-Mike
 
I

Irrwahn Grausewitz

JC said:
hi,

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {
undefined behaviour, use:

int main( void )
{
char a[2]="ab";
char b[2]="cd";
char c[4]=" \0";
Undefined behaviour, you reserved to less memory for a, b and c.
Remember: "xy" is a string literal consisting of _three_ characters,
'x', 'y' and the implicit terminating '\0'. The explicit use of '\0'
in the initializer for c is unnecessary.

char a[3]="ab";
char b[3]="cd";
char c[5]=" ";

strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding? anything wrong?
please help!!.
See above.
ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
abcd"
This worked only by chance, see above.

Regards

Irrwahn
 
R

Robert B. Clark

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {

int main(void)

main returns an int.
char a[2]="ab";

The string "ab" actually has three chars ('a', 'b' and the terminating NUL
character '\0'), but you've only allocated two in array a[].

Change this to

char a[3]="ab";

or
char a[] = "ab";

In the latter case, the compiler will allocate sufficient storage (3) for
you.
char b[2]="cd";
char c[4]=" \0";

Ditto.

Note that c[] may hold only three characters plus the terminating NUL, so
you'll want to increase the allocated space in preparation for your string
concatenation below:

char c[5] = '\0'; /* 4 chars + terminating NUL */
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);

return 0;
ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
abcd"

Buffer overrun in c.

Just because something is UB, doesn't mean that the result of that UB *has*
to be counter to one's expectations. :)
 
C

CBFalconer

JC said:
i want to combine two string together.. and put in to another
string. how can i do . i try myself.. with the follow code. but
seem can't get the result i want.. i want to get the result with
"c is abcd" .

#include <stdio.h>
#include <string.h>
void main() {
char a[2]="ab";

This is not a string. It has no trailing '\0', which it would if
the array size was 3.
char b[2]="cd";

same here.
char c[4]=" \0";

I'm not sure what this is, but it isn't a string either.
strcpy(c,a);
strcat(c,b);

You have to have strings to use strcpy and strcat.

.... snip ...
what problem to my coding? anything wrong?

Yes.
 
D

Darrell Grainger

hi,

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {

The main function should be:

int main(void)
char a[2]="ab";
char b[2]="cd";

The string "ab" is actually the array { 'a', 'b', '\0' }; When written
this way you should be able to see there are 3 elements in the array. so
change these to:

char a[] = "ab";
char b[] = "cd";
char c[4]=" \0";

Since you are copying a[] and b[] to c[] you want to end up with the array
{ 'a', 'b', 'c', 'd', '\0' }; This has five elements; not four. So just
use:

char c[5];
strcpy(c,a);
strcat(c,b);

This would work. You can also do:

sprintf(c, "%s%s", a, b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);

return 0;
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding? anything wrong?
please help!!.

Thanks

JC

ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
abcd"

Just luck.
 
I

Irrwahn Grausewitz

CBFalconer said:
JC wrote:
char c[4]=" \0";

I'm not sure what this is, but it isn't a string either.
To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.

<SNIP>

Irrwahn
 
S

Steve Zimmerman

JC said:
hi,

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {
char a[2]="ab";
char b[2]="cd";
char c[4]=" \0";
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding? anything wrong?
please help!!.

Thanks

JC

ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
abcd"



JC,


Thank you for your question.

#include <stdio.h>
#include <string.h>

int main()
{
char a[] = "ab";
char b[] = "cd";

char c[5];

strcpy(c, a); /* Instead of these two lines, you may write */
strcat(c, b); /* strcpy(c, strcat(a, b)); */

printf("%s\n", c);

return 0;
}

--Steve
 
A

Arthur J. O'Dwyer

JC,

Thank you for your question.

#include <stdio.h>
#include <string.h>

int main()
{
char a[] = "ab";
char b[] = "cd";

char c[5];

strcpy(c, a); /* Instead of these two lines, you may write */
strcat(c, b); /* strcpy(c, strcat(a, b)); */

I seem to recall having said this before, but:

STOP STOP STOP STOP STOP STOP STOP STOP STOP!!

Do *NOT* give newbies incorrect information!
It's bad!
Don't do it!
Resist the temptation!
Pick up a book and learn how to answer questions correctly!
Thank you!


[The "Instead of..." comment is utterly wrong; the code
suggested won't even "work" on typical x86 implementations --
so it's fairly obvious that the comment was the product of
either willful ignorance or maliciousness.]

-Arthur
 
R

Richard Heathfield

Arthur said:
JC,

Thank you for your question.

#include <stdio.h>
#include <string.h>

int main()
{
char a[] = "ab";
char b[] = "cd";

char c[5];

strcpy(c, a); /* Instead of these two lines, you may write */
strcat(c, b); /* strcpy(c, strcat(a, b)); */

I seem to recall having said this before, but:

STOP STOP STOP STOP STOP STOP STOP STOP STOP!!

Do *NOT* give newbies incorrect information!
It's bad!
Don't do it!
Resist the temptation!
Pick up a book and learn how to answer questions correctly!
Thank you!

It appears that you're wasting your time, Arthur. Just about every Zimmerman
article I read is flawed in some major respect, and he seems to ignore
corrections. You might as well just address the corrections directly to the
OP...
[The "Instead of..." comment is utterly wrong; the code
suggested won't even "work" on typical x86 implementations --
so it's fairly obvious that the comment was the product of
either willful ignorance or maliciousness.]

....like this, only perhaps in more detail.
 
R

Richard Heathfield

Irrwahn said:
CBFalconer said:
JC wrote:
char c[4]=" \0";

I'm not sure what this is, but it isn't a string either.
To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.

No, the string literal comprises "whatever is in quotes" plus a null
terminating character. If "whatever is in quotes" comes to exactly the
specified array size, the terminator is ignored. In this case, it matters
little, since '\0' is included in the "whatever is in quotes" bit.

In the following case:

char d[4] = "abcd";

the code is legal, d[0] is 'a', ... d[3] is 'd', and there is no null
terminator, so d is not a string.
 
J

JC

Thanks for help!!

JC

Steve Zimmerman said:
JC said:
hi,

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {
char a[2]="ab";
char b[2]="cd";
char c[4]=" \0";
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding? anything wrong?
please help!!.

Thanks

JC

ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
abcd"



JC,


Thank you for your question.

#include <stdio.h>
#include <string.h>

int main()
{
char a[] = "ab";
char b[] = "cd";

char c[5];

strcpy(c, a); /* Instead of these two lines, you may write */
strcat(c, b); /* strcpy(c, strcat(a, b)); */

printf("%s\n", c);

return 0;
}

--Steve
 
I

Irrwahn Grausewitz

Richard Heathfield said:
Irrwahn said:
CBFalconer said:
JC wrote:
char c[4]=" \0";

I'm not sure what this is, but it isn't a string either.
To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.

No, the string literal comprises "whatever is in quotes" plus a null
terminating character. If "whatever is in quotes" comes to exactly the
specified array size, the terminator is ignored. In this case, it matters
little, since '\0' is included in the "whatever is in quotes" bit.

Right, of course, but I was referring to c, not the string literal it
was initialized with. I did not express myself clearly though.

<SNIP>
 
J

JC

sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
any sample method?

please help!

thanks
Jack
 
R

Richard Heathfield

Irrwahn said:
Richard Heathfield said:
Irrwahn said:
JC wrote:
<SNIP>
char c[4]=" \0";

I'm not sure what this is, but it isn't a string either.

To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.

No, the string literal comprises "whatever is in quotes" plus a null
terminating character. If "whatever is in quotes" comes to exactly the
specified array size, the terminator is ignored. In this case, it matters
little, since '\0' is included in the "whatever is in quotes" bit.

Right, of course, but I was referring to c, not the string literal it
was initialized with. I did not express myself clearly though.

Perhaps /I/ didn't express myself clearly enough. The initialisation does
/not/ invoke undefined behaviour. The end contents of c are ' ', ' ', ' ',
'\0', and the fifth character in " \0" is /not/ used for initialisation
in this special case.
 
M

Mike Wahler

JC said:
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??

Fill with what? Space characters? Zero characters?

The following will let you pass the 'filler' character
as a function argument.
any sample method?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *copyandpad(char *dest, const char *src,
size_t dest_len, char fillchar)
{
size_t len = strlen(src);
strcpy(dest, src);
memset(dest + len, fillchar, dest_len - len - 1);
return dest;
}
int main()
{
char s1[] = "1234";
char s2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char fill = 'X'; /* adjust this value to your needs */

printf("Before: %s\n", s2);
copyandpad(s2, s1, sizeof s2, fill);
printf("After : %s\n", s2);
return 0;
}


Output:

Before: ABCDEFGHIJKLMNOPQRSTUVWXYZ
After : 1234XXXXXXXXXXXXXXXXXXXXXX

HTH,
-Mike
 
I

Irrwahn Grausewitz

Richard Heathfield said:
Irrwahn said:
Richard Heathfield said:
Irrwahn Grausewitz wrote:


JC wrote:
<SNIP>
char c[4]=" \0";

I'm not sure what this is, but it isn't a string either.

To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.

No, the string literal comprises "whatever is in quotes" plus a null
terminating character. If "whatever is in quotes" comes to exactly the
specified array size, the terminator is ignored. In this case, it matters
little, since '\0' is included in the "whatever is in quotes" bit.

Right, of course, but I was referring to c, not the string literal it
was initialized with. I did not express myself clearly though.

Perhaps /I/ didn't express myself clearly enough. The initialisation does
/not/ invoke undefined behaviour. The end contents of c are ' ', ' ', ' ',
'\0', and the fifth character in " \0" is /not/ used for initialisation
in this special case.

<slaps forehead> Most certainly I didn't engage my brain before reading.
:)

Irrwahn
 
G

goose

JC said:
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
any sample method?

put it at the beginning of the 26 char string ?

1. use memset() to set all 26 elements of your
destination string to the space character.
2. use assignment to set the last element of
your destination array to 0 (zero) so that it
is a valid string.
3. use memcpy() to copy 4 chars from the 4 element
string to the destination string.

check your textbook to find out what memset and memcpy do.

goose,
hth
 
B

Barry Schwarz

sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
any sample method?
When you say 4 char, does that include the terminating '\0' or not?

If you strcpy a string with 4 characters to an array of 26 char, the 4
(or 5) characters of the string (including the '\0') will be copied to
the first 4 (or 5) elements of the array. The remainder of the array
is untouched. If you pass this array to any string function or use it
in any string context, only the 4 (or 5) char in the string
participate. There is no need to fill in the "extraneous" portion of
the array.

If you want the resulting string to actually be 26 char long
(including the '\0') and you want to pad the right end of the string
with some char, you could do something like

char c = '?'; /*whatever pad character you like*/
size_t len;
len = strlen(array);
if (len < 25){
memset(array+len, c, 25-len);
array[25] = '\0';
}


<<Remove the del for email>>
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top