strcpy/strcat

S

Seebs

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
 
T

Trance

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

Tech07

Trance said:
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). :)
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top