Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strcpy/strcat

Reply
Thread Tools

strcpy/strcat

 
 
Seebs
Guest
Posts: n/a
 
      09-15-2009
On 2009-09-15, Trance <(E-Mail Removed)> wrote:
> I have a quick question related to strcpy/strcat.
>
> Let's say I allocated memory for a string named string1. Now, I copy
> another string, string2, at string1. I allocated more memory for
> string1 than length of string2 +1. Now, I append another string,
> string3, to string1. Also, I make sure there's enough memory allocated
> in the first plate for string1. Namely:
>
> strcpy(string1, string2);
> strcat(string1, string3);
>
> Now, if copy yet another string2a into string1, and then concatenate
> with yet another string string3a, I'm curious what will happen.
> Namely, I follow the above code with:
>
> strcpy(string1, string2a);
> strcat(string1, string3a);
>
> The question is, will the string3a be appended after string 2a, or
> after string2a and string3? I assume that after doing the strcpy
> statement above with string1 and string2a, there will be a null
> character overwriting the first character of original string3.


I don't know. There will be a null character at the same distance into
string1 that there was in string2a. If string2a is shorter than string2,
that will not be the first character of original string3, which will still
be sitting there untouched.

> Is this
> correct? Also, how does strcat behave, will it start append at the
> first null character of the destination string or after the last
> nonzero character of the destination string? I would say it's the
> former, as the initial content of the memory when allocating string1
> could be non-zero garbage (unless one uses calloc)


Right you are. The trick is to realize that the "string" is simply
the set of characters up to the first null -- not the total buffer that
they are contained in.

> A more general questions, do strcat and strcpy take care of any memory
> allocation/deallocation?


No.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
Trance
Guest
Posts: n/a
 
      09-15-2009
Hello all,

I have a quick question related to strcpy/strcat.

Let's say I allocated memory for a string named string1. Now, I copy
another string, string2, at string1. I allocated more memory for
string1 than length of string2 +1. Now, I append another string,
string3, to string1. Also, I make sure there's enough memory allocated
in the first plate for string1. Namely:

strcpy(string1, string2);
strcat(string1, string3);

Now, if copy yet another string2a into string1, and then concatenate
with yet another string string3a, I'm curious what will happen.
Namely, I follow the above code with:

strcpy(string1, string2a);
strcat(string1, string3a);

The question is, will the string3a be appended after string 2a, or
after string2a and string3? I assume that after doing the strcpy
statement above with string1 and string2a, there will be a null
character overwriting the first character of original string3. Is this
correct? Also, how does strcat behave, will it start append at the
first null character of the destination string or after the last
nonzero character of the destination string? I would say it's the
former, as the initial content of the memory when allocating string1
could be non-zero garbage (unless one uses calloc)

A more general questions, do strcat and strcpy take care of any memory
allocation/deallocation?

Thanks very much.
 
Reply With Quote
 
 
 
 
Tech07
Guest
Posts: n/a
 
      09-16-2009
Trance wrote:
> Hello all,
>
> I have a quick question related to strcpy/strcat.
>
> Let's say I allocated memory for a string named string1. Now, I copy
> another string, string2, at string1. I allocated more memory for
> string1 than length of string2 +1. Now, I append another string,
> string3, to string1. Also, I make sure there's enough memory allocated
> in the first plate for string1. Namely:
>
> strcpy(string1, string2);
> strcat(string1, string3);
>
> Now, if copy yet another string2a into string1, and then concatenate
> with yet another string string3a, I'm curious what will happen.
> Namely, I follow the above code with:
>
> strcpy(string1, string2a);
> strcat(string1, string3a);
>
> The question is, will the string3a be appended after string 2a, or
> after string2a and string3? I assume that after doing the strcpy
> statement above with string1 and string2a, there will be a null
> character overwriting the first character of original string3. Is this
> correct? Also, how does strcat behave, will it start append at the
> first null character of the destination string or after the last
> nonzero character of the destination string? I would say it's the
> former, as the initial content of the memory when allocating string1
> could be non-zero garbage (unless one uses calloc)
>
> A more general questions, do strcat and strcpy take care of any memory
> allocation/deallocation?
>
> Thanks very much.


Apparently, some unsuspecting "youths" are still pursuing learning C! Is
that bizarre or what? Oh, it means "youth is wasted on the young", huh.

I invented roller blades (in my pre-teen years).


 
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




Advertisments