home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: extern: interesting case...
- Keywords: extern
- Message-ID: <4311@dozo.and.nl>
- Date: 7 Jan 93 15:49:16 GMT
- References: <bibhas.726341439@femto.engr.mun.ca>
- Organization: AND Software BV Rotterdam
- Lines: 45
-
- In article <bibhas.726341439@femto.engr.mun.ca> bibhas@pico.engr.mun.ca (Bibhas Bhattacharya) writes:
- |When I declare:
- |extern char line[];
- |for the variable "line" which was defined in another file as:
- |char line[80];
- |everything works fine. But when I do:
- |extern char *line;
- |the program dumps core. I can't figure out the difference, especially when
- |extern declaration is not supposed to reserve any memory or anything. When
- |I looked up K&R, the correct declaration would be: extern char line[];.
- |
- |Can anyone please clarify the case.
-
- A pointer is an object that can contain the address of another object:
- it `points' to the refered object. An array is simply a chunk of con-
- tiguous memory refered to by its name. So:
-
- char line[80];
-
- defines a piece of memory (80 bytes long) and whenever you mention the
- name `line', the compiler knows that you refer to those 80 bytes of memory.
-
- On the other hand:
-
- char line[80];
-
- char *linep= line;
-
- tells the compiler that the object named `linep' is a pointer, pointing
- to an 80 bytes wide piece of memory that can also be refered to as `line'.
-
- So, if you tell the compiler that there exists a piece of memory somewhere,
- named `line' (extern char line[];) and somewhere else you tell the compiler
- that there exists an object that can _point_ to chunk of memory (extern
- char *line;) things get mixed up. If `line' is defined (in some source file)
- as a chunk of memory, and somewhere else you trick the compiler, making
- it believe that `line' is just an object that contains an _address_ of
- something else, what else would you expect besides a core dump or a locked
- up process or any other disaster you can come up with? ;-)
-
- Does this clarify things a bit?
-
- kind regards,
-
- Jos aka jos@and.nl
-