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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!munnari.oz.au!metro!mama!radics!zoltan
  3. From: zoltan@research.canon.oz.au (Zoltan Kocsi)
  4. Subject: Re: extern: interesting case...
  5. In-Reply-To: bibhas@pico.engr.mun.ca's message of Wed, 6 Jan 1993 17:30:39 GMT
  6. Message-ID: <ZOLTAN.93Jan8133952@radics.research.canon.oz.au>
  7. Originator: zoltan@radics
  8. Sender: news@research.canon.oz.au
  9. Organization: Canon Information Systems Research, Australia
  10. References: <bibhas.726341439@femto.engr.mun.ca>
  11. Date: Fri, 8 Jan 1993 03:39:52 GMT
  12. Lines: 51
  13.  
  14. In article <bibhas.726341439@femto.engr.mun.ca> bibhas@pico.engr.mun.ca (Bibhas Bhattacharya) writes:
  15.  
  16.    When I declare:
  17.    extern char line[];
  18.    for the variable "line" which was defined in another file as:
  19.    char line[80];
  20.    everything works fine. But when I do:
  21.    extern char *line;
  22.    the program dumps core. I can't figure out the difference, especially when
  23.    extern declaration is not supposed to reserve any memory or anything. When
  24.    I looked up K&R, the correct declaration would be: extern char line[];.
  25.  
  26.    Can anyone please clarify the case.
  27.    Thanks.
  28.    Bibhas
  29.  
  30. Well, the K&R book tells you the truth. The declaration of an external object
  31. tells the compiler the type and name of the object and the fact that the
  32. actual memory allocation of the object is done in an other file.
  33. That's why, when you write extern char x[] it declares an array of chars
  34. named x. The actual size of the array is not required as
  35. the compiler need not to allocate memory for it. This declaration
  36. declares x as an array, that is, a memory location where _the_characters_in_
  37. the_array_ can be found. On the other hand, if you declare 'extern char *y' it
  38. tells the compiler that y is pointer ( a (probably) four-byte object ), the
  39. pointed object is character and the memory for the _four_bytes_ is allocated
  40. somewhere else. So when you write x[ 3 ], the compiler gets the address that
  41. x itself symbolyses, adds 3 to it then accesses the byte at this address.
  42. That's what you want. When you wrote y[ 3 ], the compiler got the address
  43. meant by y, then read four bytes from this location (dereferenced the pointer)
  44. handled this value as the start address of the array, added 3 to it and
  45. accessed the byte at this address. Because at memory location y there was not
  46. a pointer but some ASCII characters, the result address was rubbish, and you
  47. were lucky that it pointed out of your memory segment causing the coredump.
  48. Though the K&R book emphasyses the relationship between arrays and pointers,
  49. it never tells that they are the same. In fact, you can write y = &my_char;
  50. but you can never change the value of x ( being a symbolic constant ).
  51. Also, &x == &(x[0]) == x but &y != &(y[0]) == y. 
  52. There is a case in C where the two declarations identical:
  53. If you have a function that has an input parameter of an array you can really
  54. declare it either x[] or *y. It is because C never passes
  55. arrays to functions, it _always_ passes the address of the array, that is
  56. a pointer to the array. In that sense the declaration x[] is misleading as
  57. the object you got from the caller is _a_pointer_ and not an array. (The
  58. compiler internally transforms your x[] declaration to *x if it declares
  59. an input parameter of a function.)
  60.  
  61. Zoltan.
  62.     
  63. -- 
  64. Zoltan Kocsi <zoltan@research.canon.oz.au>
  65.