Mail Archives: djgpp/1997/06/13/00:24:55
Springman wrote:
> The *argv[] is used more probably because it is safer. Why?
> A basic example:
> void func1 (int *p);
> void func2 (int p[]);
> Both func1 and func2 accept pointers to ints as arguments. But they are
> not exactly the same. The func2 takes a pointer that is constant, i.e.
> you'll get warnings when you try to modify its value.
No, you won't. The ANSI C specification specifically states that an array
argument decays to a pointer argument. No implicit consts are introduced
by the compiler. As such, the following function is perfectly legal:
void f(int a[])
{
static int x;
a = &x;
}
The reason is because arrays are automatically treated as pointers in
function arguments, so this function is in fact treated as
void f(int *a) /* ... */
and as such using the argument as an lvalue is perfectly legal. (Perhaps
not very useful, though, because its value is only being changed within
the function.)
Your statement is tantamount to stating that the int a[] argument would be
silently treated as a int *const, which is not correct.
--
Erik Max Francis, &tSftDotIotE / email / max AT alcyone DOT com
Alcyone Systems / web / http://www.alcyone.com/max/
San Jose, California, United States / icbm / 37 20 07 N 121 53 38 W
\
"Covenants without the sword / are but words."
/ Camden
- Raw text -