On Sat, 2006-09-23 at 13:49 -0600, Dave Smith wrote:
> Wrong. In both cases (char* and char[]), the variable itself stores the
> address. You do not need the & operator in either case.
>
> In fact, using the & operator is incorrect, because it will give you the
> address of the variable that was already holding the address of the
> character array, instead of the address of the character array.
According to my informal testing, if the array is statically declared
like this:
char myarray[50];
then &myarray and myarray seem to be equivalent. However, in the case
of:
char *myarray="something"
then &myarray would certainly be wrong, since that would be a pointer to
a pointer. Which is what the grandfather post said. I do agree, though
that the & operator is incorrect to use, even it if does work.
It's trickier when you are trying to deal with implementing some sort of
pass-by-reference in C. Usually you pass pointers to pointers. But for
static arrays you don't usually pass by pointer at all (at least
explicitly); it's already pass-by-pointer. This would be one of the
cases where char[] and char* are *not* equivalent. For example,
char *myarray1="something"; //pointer to a static character array
char myarray2[50];
strncpy(myarray2,"something",50);
test1(myarray1); //fine
test1(myarray2); //fine
test2(&myarray1); //fine; myarray1 will be pointing at something else
test2(&myarray2); //will not work since char[] and char* are different,
although it would compile I think
test3(myarray1); //don't think this works but I'm not sure
test3(myarray2); //fine.
void test1(char *a){
printf("%s\n",a);
}
void test2(char **a) {
(*a)="another static string";
}
void test3(char a[]) {
printf("%s",a);
}
Michael
>
> --Dave
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
>
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/