Wording in K&R Strcpy code about pointers being passed by value

1 month ago 29

On page 105, the authors provide the following example:

/* Strcpy: copy t to s */ void Strcpy(char *s, char *t){ while ((*s = *t) != '\0'){ s++; t++; } }

The authors state:

Because arguments are passed by value, Strcpy can use the parameters s and t in any way it pleases.

Should there not be a caveat here that this freedom only applies to s and not t? For instance,

t[0] = '4'; //inside of Strcpy

would be disastrous at the caller site.

That is, even though t within strcpy is a copy of whatever pointer is the actual argument (say T) at the calling site, i.e., aren't the following asserts valid

assert(&t != &T);//so, t is "different" from T assert(t == T);//yet they point to the same address in memory

Godbolt link of above : https://godbolt.org/z/Ycfxfess6

So, there extends to s some freedoms (for e.g., one can arbitrarily write into it garbage before doing the true copy) which t does not enjoy.

submitted by /u/onecable5781 to r/C_Programming
[link] [comments]
Read Entire Article