Re: string buffer by George
George
Mon Dec 10 19:27:01 PST 2007
Hi Tom,
> > strcpy (buf, array);
> > strcpy (&buf, array);
>
> The second shouldn't compile, since strcpy requires a char*. &buf[0]
> will compile, but it's usual to use just buf where possible, such as here.
>
Both can compile in C, but only the 1st one can compiler in C++. In C, both
buf and &buf have the same value, and this is why I am most confused. Why a
variable is the same as the pointer of it?
regards,
George
"Tom Widmer [VC++ MVP]" wrote:
> George wrote:
> > Hello everyone,
> >
> >
> > Suppose we defined a string buffer (array), like this,
> >
> > char array[] = "hello world";
> > char buf[256]
> >
> > Sometimes, I noticed that we either use,
> >
> > 1. array (buf)
>
> The name of an array decays to a pointer to the first element (the
> pointer has type char* in this case), except when used in certain
> contexts, such as binding to a reference or in a sizeof
>
> > or use,
> > 2. &array (&buf)
>
> That takes the address of the array (which is numerically the same
> pointer value as the address of the first element), which has type: char
> (*)[12] in the first case and char (*)[256] in the second.
>
> > or use
> > 3. &array[0] (&buf[0])
>
> This is identical to 1, except where pointer decay doesn't apply to 1.
>
> > as the beginning address of the array,
> >
> > example like,
> >
> > strcpy (buf, array);
> > strcpy (&buf, array);
>
> The second shouldn't compile, since strcpy requires a char*. &buf[0]
> will compile, but it's usual to use just buf where possible, such as here.
>
> Tom
>