[NTLUG:Discuss] Re: OT C question
Ian Donaldson
tartled at gmail.com
Mon Feb 20 16:08:07 CST 2006
> >> I just recently ran into this funny situation and I don't understand
> >> the fix very well. Here is the example I was working with:
> >>
> >> extern unsigned char *myarray;
> >>
> >> void myfunc(unsigned char *somearray);
> >>
> >> void main(void)
> >> {
> >> myfunc(&myarray);
> >> myarray[0] = '*';
> >> *(myarray+1) = '*';
> >> }
> >>
> >> 1> The function call only worked when the & was added in front of
> >> myarray. This doesn't make any kind of sense to me
The code in main.c, believes that the variable is a pointer, so it
retreives the contents of location "myarray", instead of the address
of the variable myarry, which is what you want.
char *myarray = "Hello World";
and
char myarray[] = "Hello World";
are only seem to be equivalent because the compiler has initialized
the pointer in the first statement to point to the data.
More information about the Discuss
mailing list