home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12658 < prev    next >
Encoding:
Internet Message Format  |  1992-08-22  |  4.2 KB

  1. Path: sparky!uunet!decwrl!csus.edu!netcom.com!netcomsv!spacebbs!ted.jensen
  2. From: ted.jensen@spacebbs.com (Ted Jensen) 
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer/address reluctance
  5. Message-ID: <10444.610.uupcb@spacebbs.com>
  6. Date: 22 Aug 92 14:47:00 GMT
  7. Distribution: na
  8. Organization: SPACE BBS - Menlo Park, CA - 10 Lines + 4gB - 415-323-4193
  9. Reply-To: ted.jensen@spacebbs.com (Ted Jensen) 
  10. Lines: 96
  11.  
  12.  
  13. While I did not see the original post, I have followed the last
  14. half dozen or so comments in this thread and would like to
  15. state that I have been frustrated with the insistence on many
  16. writers (including K&R) to refer to an address as a pointer.
  17. IMHO, I find it much easier to think of an address as a value
  18. which identifies a location in memory, and a pointer as a
  19. variable designed to hold a value which identifies a location in
  20. memory, i.e. an address.
  21.  
  22. Based on the above definitions (which I feel few could argue
  23. with), a pointer is an lvalue whereas an address is not.  Thus,
  24. there is a distinct difference between the two.  What is
  25. frustrating is when a writer states that a function or
  26. calculation "returns a pointer".
  27.  
  28. The problem, of course, stems from the failure to distinguish
  29. between an integer variable, and an integer value, a float
  30. variable and a float value, etc.  Consider the following:
  31.  
  32. int j,k;
  33.  
  34. const int m = 23;
  35.  
  36. j = 12;
  37.  
  38. k = 17 * j;
  39.  
  40. Both j and k are integer variables.  Space has been reserved in
  41. the data area for storage of these variables and the address of
  42. this space can be obtained using &j or &k respectively.  The
  43. contents of each of these respective memory locations are subject
  44. to change during the course of the program, hence the use of the
  45. term "variable".
  46.  
  47. Now, space has also been reserved in memory to hold the value of
  48. the constant m.  It is expected that this value will not change
  49. throughout the life of the program but will be available to any
  50. number of places throughout the program (if visible).
  51.  
  52. Now, looking at the "17" in the statement that follows the
  53. variable definitions, we note that it represents a specific
  54. integer value.  In this case, it is also an integer constant (i.e.
  55. unchanging throughout the life of the program).  However, it is
  56. not stored in the data space, it is "hard-wired" into the code
  57. space itself.  Most, however, would simply refer to 17 as an
  58. integer (changing from the semantics of C to the semantics of
  59. mathematics without a second thought).
  60.  
  61. But what of the value returned by the multiplication of the 17
  62. times the current value of j?  This value is 204.  While this
  63. value may at some point in the actual running of the program end
  64. up being stored on the stack or in a register within the CPU, it
  65. has no "address" in the data space (excluding the stack) which
  66. can be retrieved through the use of the & operator.
  67.  
  68. Is it an integer variable?  (it may not be constant throughout
  69. the life of the program since it is dependent on the value of j).
  70. Is it an "object"?  Is it an integer constant?  In short, while
  71. we can correctly state that it is of "type" integer I find it
  72. difficult to come up with words which would better define it,
  73. with the possible exception of "integer value".
  74.  
  75. The same arguments could, of course, be made for a double, a
  76. float, or a character.
  77.  
  78. Keeping all of the above in mind we now come to the question at
  79. hand.  Is there a difference between a pointer and an address?
  80. As I stated in my opening argument, unless specifically stated I
  81. think of a pointer as a variable occupying space in the data
  82. space of memory and designed to hold an address.  Thus, unless we
  83. are talking about a pointer constant, a pointer is an lvalue and
  84. an address is not.
  85.  
  86. Whenever I see something like:
  87.  
  88.   int *ptr;
  89.   int array[25];
  90.  
  91.   ptr = array;
  92.  
  93. and the writer writes something like:  "The reason we can do this
  94. is that the name of an array returns a pointer."  it effects me
  95. in the same manner as fingernails on chalk board!  Using
  96. "..returns a pointer constant" is a little better,  but what is
  97. wrong with  "... returns the address of the first element in the
  98. array." ???
  99.  
  100. "array" is not a pointer, it is an address!  (IMHO!)
  101.  
  102. Similarly, I think of functions as returning addresses, not
  103. pointers, and calls to them as passing addresses, not pointers.
  104. >>> Continued to next message
  105. ---
  106.  * SLMR 2.0 * 
  107.                                               
  108.