Path: utzoo!attcan!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: Difference between "char *arr" and "char arr[]" Message-ID: <1990Sep22.130345.15450@virtech.uucp> Date: 22 Sep 90 13:03:45 GMT References: <8103@aggie.ucdavis.edu> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 32 In article <8103@aggie.ucdavis.edu> kuan@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