Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: strcpy ques...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 17 Topic 26073 of 26967
Post > Topic >>

Re: strcpy question

by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= <toe@[EMAIL PROTECTED] > May 5, 2008 at 11:39 AM

On May 5, 7:19=A0pm, mdh <m...@[EMAIL PROTECTED]
> wrote:

> void strcpy( char *s, char *t){
>
> while ( *s++ =3D *t++);
>
> }
>
> Could someone help me understand the library function, which,
> according to the appendix, returns the target string.


Well firstly, this particular implementation of it does NOT return the
target string.

Let's take a look at that loop:

    while (*s++ =3D *t++);

This is the same as the following:

    for (;;)  /* Eternal loop */
    {
        *s =3D *t;

        ++s;
        ++t;

        if (!*(s-1)) break;
    }

It copies the destination to the source, increments both pointers, and
finally checks to see whether the last character copied was the null
character. If so, the loop stops.


> So, in this case, what is passed to the library is an array-the
> original string =A0and a pointer to the target string? What happens if
> the target pointer has not been allocated space? And, is this
> acceptable ie to pass a pointer instead of an actual char array for
> the target string?


If you have an array as follows:

    char arr[16];

then when you write the name of the array on its own, what you have is
a pointer to the first element of the array, e.g.:

    char arr[16];

    char *p;

    p =3D arr;  /* Here we have a pointer to the first element */

(There are three special cases in which this isn't so:
    1) When sizeof is applied to the array
    2) When the addressof operator is applied to the array
    3) ...I can't actually think of it off-hand, but I'm sure I'd know
it if I was presented with it.)

A pointer contains a memory address. When you invoke strcpy, you're
giving it two memory addresses, the address of the destination and the
address of the source. If the either pointer is dodgy, you'll get
undefined behaviour. For a pointer not to be dodgy, it must:
1) Point to memory that belongs to you, that is, memory that you're
allowed to access.
2) Point to a big enough chunk of memory to store what you want it to
store. If you go outside the boundary, you're writing to memory that
doesn't belong to you.
 




 17 Posts in Topic:
strcpy question
mdh <mdeh@[EMAIL PROTE  2008-05-05 11:19:59 
Re: strcpy question
Eric Sosman <Eric.Sosm  2008-05-05 14:39:35 
Re: strcpy question
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-05 11:39:39 
Re: strcpy question
David Thompson <dave.t  2008-05-19 03:59:51 
Re: strcpy question
Keith Thompson <kst-u@  2008-05-18 21:39:15 
Re: strcpy question
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-05 11:41:33 
Re: strcpy question
"Default User"   2008-05-05 18:47:03 
Re: strcpy question
mdh <mdeh@[EMAIL PROTE  2008-05-05 13:08:56 
Re: strcpy question
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-05 14:34:55 
Re: strcpy question
mdh <mdeh@[EMAIL PROTE  2008-05-05 16:04:37 
Re: strcpy question
"Default User"   2008-05-05 23:16:18 
Re: strcpy question
Keith Thompson <kst-u@  2008-05-05 16:23:24 
Re: strcpy question
mdh <mdeh@[EMAIL PROTE  2008-05-05 16:07:12 
Re: strcpy question
mdh <mdeh@[EMAIL PROTE  2008-05-05 16:52:00 
Re: strcpy question
Keith Thompson <kst-u@  2008-05-05 18:00:22 
Re: strcpy question
mdh <mdeh@[EMAIL PROTE  2008-05-05 16:52:33 
Re: strcpy question
mdh <mdeh@[EMAIL PROTE  2008-05-05 18:17:13 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 6:42:11 CDT 2008.