Difference between "char *arr" and "char arr[]"
Conor P. Cahill
cpcahil at virtech.uucp
Sat Sep 22 23:03:45 AEST 1990
In article <8103 at aggie.ucdavis.edu> kuan at iris.ucdavis.edu (Frank [Who me?] Kuan) writes:
>Now, I always thought that "targ[]" and "char *targ" were equivalent.
>I have several places in my program where I use those two notations
>interchangeably, and this was the first time I've ever had a problem
>with it.
They are equivalent (with respect to referencing data) for just about all
cases EXCEPT for global variables.
The reason that they are equivalent for function calls is that the array is
acutally converted to (and passed as) a pointer.
For global variables "targ" will represent a location in memory assigned
by the linker. Any mention of the variable "targ" will be replaced by
an absolute memory location by the linker. The contents of this memory
location will differ depending upon the declaration of targ.
If it is declared as "char * targ;",
the compiler will expect a character pointer at this location
If it is declared as "char targ[xx];"
the compiler will expect a character arrry starting at this
location
>correct practice is so I don't run into these horrible bugs again?
The correct practice is that global declarations and references much
match exactly (except initializations of course).
--
Conor P. Cahill (703)430-9247 Virtual Technologies, Inc.,
uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160
Sterling, VA 22170
More information about the Comp.lang.c
mailing list