Re: Doubt about array's name
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Wed, 30 Apr 2008 14:14:52 +0000
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: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- Follow-Ups:
- Re: Doubt about array's name
- From: nembo kid
- Re: Doubt about array's name
- References:
- Doubt about array's name
- From: nembo kid
- Doubt about array's name
- Prev by Date: Re: Temporarily close stdout?
- Next by Date: Re: taking a "word" as input
- Previous by thread: Doubt about array's name
- Next by thread: Re: Doubt about array's name
- Index(es):
Relevant Pages
|