E. Robert Tisdale said:
Now that you've caught up with everyone else,
Don't be unpleasant, especially when you're wrong.
please explain
Delighted.
Syntax error.
#include <stdio.h>
int f(char* s[10]) {
char** p = s;
fprintf(stdout, "%d = sizeof(s)\n", sizeof(s));
fprintf(stdout, "%d = sizeof(p)\n", sizeof(p));
At the very least, on systems where a size_t is larger than an int the
behaviour is undefined.
Note that your demo program doesn't meet the requirements of the question,
which nowhere mentions char *s[10]. Still, never mind that. Observe:
rjh@tux:~/scratch> cat foo.c
#include <stdio.h>
void foo(char **r, char *s[], char *t[10])
{
printf("sizeof r = %lu\n", (unsigned long)sizeof r);
printf("sizeof s = %lu\n", (unsigned long)sizeof s);
printf("sizeof t = %lu\n", (unsigned long)sizeof t);
}
int main(int argc, char **argv)
{
if(argc >= 10)
{
foo(argv, argv, argv);
}
else
{
char *tmp[10];
foo(argv, argv, tmp);
}
return 0;
}
rjh@tux:~/scratch> ./foo 1 2 3 4 5 6 7 8 9 10
sizeof r = 4
sizeof s = 4
sizeof t = 4
Now for some history. Here are the first few lines of the source code from a
***very*** early C compiler. Regular readers will note the irony of my
quoting from it:
/* C compiler
Copyright 1972 Bell Telephone Laboratories, Inc.
*/
ossiz 250;
ospace() {} /* fake */
init(s, t)
char s[]; {
extern lookup, symbuf, namsiz;
char symbuf[], sp[];
int np[], i;
i = namsiz;
sp = symbuf;
while(i--)
if ((*sp++ = *s++)=='\0') --s;
np = lookup();
*np++ = 1;
*np = t;
}
Note the usage of np, which is defined as int np[], and used like this:
*np++ = 1;
Clearly, this is pointer syntax. [] was, in fact, used for pointer notation
early in C's development. Eventually, it was changed, but the usage of []
in function parameter lists lives on. It /still/ means pointer, in that
context.
Brian W Kernighan writes, in K&R2 starting at the foot of p99, "As formal
parameters in a function definition, char s[]; and char *s; are equivalent;
we prefer the latter because it says more explicitly that the parameter is
a pointer."
I don't expect Mr Tisdale to understand this, of course, but some other
people might find it interesting.