home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19370 < prev    next >
Encoding:
Text File  |  1993-01-07  |  2.0 KB  |  57 lines

  1. Path: sparky!uunet!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: extern: interesting case...
  5. Keywords: extern
  6. Message-ID: <4311@dozo.and.nl>
  7. Date: 7 Jan 93 15:49:16 GMT
  8. References: <bibhas.726341439@femto.engr.mun.ca>
  9. Organization: AND Software BV Rotterdam
  10. Lines: 45
  11.  
  12. In article <bibhas.726341439@femto.engr.mun.ca> bibhas@pico.engr.mun.ca (Bibhas Bhattacharya) writes:
  13. |When I declare:
  14. |extern char line[];
  15. |for the variable "line" which was defined in another file as:
  16. |char line[80];
  17. |everything works fine. But when I do:
  18. |extern char *line;
  19. |the program dumps core. I can't figure out the difference, especially when
  20. |extern declaration is not supposed to reserve any memory or anything. When
  21. |I looked up K&R, the correct declaration would be: extern char line[];.
  22. |
  23. |Can anyone please clarify the case.
  24.  
  25. A pointer is an object that can contain the address of another object:
  26. it `points' to the refered object. An array is simply a chunk of con-
  27. tiguous memory refered to by its name. So:
  28.  
  29. char line[80];
  30.  
  31. defines a piece of memory (80 bytes long) and whenever you mention the
  32. name `line', the compiler knows that you refer to those 80 bytes of memory.
  33.  
  34. On the other hand:
  35.  
  36. char line[80];
  37.  
  38. char *linep= line;
  39.  
  40. tells the compiler that the object named `linep' is a pointer, pointing
  41. to an 80 bytes wide piece of memory that can also be refered to as `line'.
  42.  
  43. So, if you tell the compiler that there exists a piece of memory somewhere,
  44. named `line' (extern char line[];) and somewhere else you tell the compiler
  45. that there exists an object that can _point_ to chunk of memory (extern
  46. char *line;) things get mixed up. If `line' is defined (in some source file)
  47. as a chunk of memory, and somewhere else you trick the compiler, making
  48. it believe that `line' is just an object that contains an _address_ of
  49. something else, what else would you expect besides a core dump or a locked
  50. up process or any other disaster you can come up with? ;-)
  51.  
  52. Does this clarify things a bit?
  53.  
  54. kind regards,
  55.  
  56. Jos aka jos@and.nl
  57.