CyberPsychotic wrote:
> here I was writing some thing which confused me abit. Here it is:
>
> I have two pieces of code, let it be foo.c and bar.c, in first one i have
>
> #define FOO "...."
>
> and then
>
> char lolo[]=FOO;
>
>
> which is supposed to make an array of chars containing "...." and point
> lolo to FOO, right?
> here is foo.c
>
> gizmo:~/coding/fun$ cat foo.c
> #include <stdio.h>
>
>
>
> #define FOO "...."
>
>
> char lolo[]=FOO;
> void show_extern(void);
>
> void main (void) {
>
> printf("lolo is %s\n and hex is %X\n",lolo,lolo);
> show_extern();
>
> }
>
> -------
> now bar :
>
> I just want to use the same lolo here, so I declare it as extern :
>
>
> gizmo:~/coding/fun$ cat bar.c
>
> #include <stdio.h>
>
> extern char *lolo;
Wrong. Try:
extern char lolo[];
Pointers and arrays are not the same thing. Although they can be used
interchangeably in *some* contexts, this *isn't* one of them.
With
extern char lolo[];
the linker will treat the address which is associated with the symbol
`lolo' as the address of the first character in the array.
OTOH, with
extern char *lolo;
the linker will treat the address which is associated with the symbol
`lolo' as the address of a pointer (i.e. a 4/8 byte integer).
Also, with `extern char *lolo', you would be able to do
lolo = <something>;
i.e. assign a different value to the pointer variable `lolo'. However,
with `extern char lolo[]', you can't do this, as `lolo' is effectively
a constant and not a variable.
--
Glynn Clements <[EMAIL PROTECTED]>