Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strcpy and strcat problem

Reply
Thread Tools

strcpy and strcat problem

 
 
JC
Guest
Posts: n/a
 
      09-26-2003
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"


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-26-2003

"JC" <(E-Mail Removed)> wrote in message news:bl1pas$(E-Mail Removed)-cable.com...
> 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.

> abcd"


Only by accident.

-Mike


 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-26-2003
"JC" <(E-Mail Removed)> wrote:

>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
--
ERROR 103: Dead mouse in hard drive.
 
Reply With Quote
 
Robert B. Clark
Guest
Posts: n/a
 
      09-26-2003
On Sat, 27 Sep 2003 00:37:31 +0800, "JC" <(E-Mail Removed)> wrote:

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

--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-26-2003
JC wrote:
>
> 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.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      09-26-2003
On Sat, 27 Sep 2003, JC wrote:

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

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-26-2003
CBFalconer <(E-Mail Removed)> 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.

<SNIP>

Irrwahn
--
ERROR 103: Dead mouse in hard drive.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-26-2003

"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> > char c[4]=" \0";

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


Sure it is. Three spaces followed by a terminator.

Same as

char c[] = " ";

-Mike


 
Reply With Quote
 
Steve Zimmerman
Guest
Posts: n/a
 
      09-27-2003
JC wrote:

> 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

 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      09-27-2003

On Sat, 27 Sep 2003, Steve Zimmerman wrote:
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[newbie] strcpy, strtok and strcat problem... =?ISO-8859-1?Q?Une_b=E9vue?= C Programming 16 09-01-2006 06:45 AM
Problem with strcat, strcpy,sprintf diego.arias.vel@gmail.com C Programming 24 10-30-2005 11:16 PM
strcpy and strcat's return type zeroDontSpamtype@yahoo.com C Programming 14 08-22-2005 05:01 PM
Two Questions about "strlen", "strcat" and "strcpy" Matt C Programming 82 09-29-2004 12:26 PM
Difference between strcpy() and strcat()? Pascal Damian C Programming 9 03-06-2004 05:38 AM



Advertisments