comp.lang.c
[Top] [All Lists]

Re: Doubt about array's name

Subject: Re: Doubt about array's name
From: Richard Heathfield
Date: Wed, 30 Apr 2008 14:14:52 +0000
Newsgroups: comp.lang.c

nembo kid said:

> In the following function, [shouldn't s] be a pointer costant (array's
> name)?

It's just a copy. C is pass-by-value. When the argument expression is 
evaluated (in the *call* to chartobyte()), the array name is treated as if 
it were a pointer to the first element in the array, and this pointer 
value is copied into the parameter object that the implementation 
constructs for the call.

> So why it is legal its increment?

Why not? It's only a copy, after all. It doesn't affect the original array 
address in any way whatsoever.

> /* Code starts here */
> void chartobyte (char *s) {
> 
> while (s!=0) {
>   printf ("%d", *s);
>   s++;
> }
> /* Code ends here */

I think you meant to write:

void chartobyte(const char *s) /* significant difference #1 */
{
  while(*s != '\0') /* significant difference #2 */
  {
    printf("%d", *s);
    s++;
  }
}

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -                                www">http://www. +rjh@
Google users: <                                www.cpax.org.uk/prg/writings/googly.php">http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy