home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / pascal / 8047 < prev    next >
Encoding:
Text File  |  1993-01-11  |  1.7 KB  |  70 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!nott!cunews!genesis.sce.carleton.ca!gboersma
  3. From: gboersma@sce.carleton.ca (Gerald Boersma)
  4. Subject: Re: pointers to arrays?!?
  5. Message-ID: <gboersma.726780493@genesis.sce.carleton.ca>
  6. Sender: news@cunews.carleton.ca (News Administrator)
  7. Organization: Carleton University
  8. References: <1993Jan11.205412.1@cc.newcastle.edu.au> <1is7qmEINNfgi@uni-erlangen.de>
  9. Date: Mon, 11 Jan 1993 19:28:13 GMT
  10. Lines: 60
  11.  
  12. gedeck@pctc.chemie.uni-erlangen.de (Peter Gedeck) writes:
  13.  
  14.  
  15. >|>Type
  16. >|>   ArrayType = Array[1..Max] of Char;
  17. >|>
  18. >|>Var
  19. >|>   ArrayVar  : ^ArrayType;
  20. >|>   Index     : Word;    
  21. >|>
  22. >|>[..]
  23. >|>
  24. >|>ArrayVar[Index]^ := etc..????
  25.  
  26. >ArrayVar^[Index] := etc..
  27. >      
  28. >Peter Gedeck
  29.  
  30. In C, it is possible to do the following with arrays on the heap:
  31.  
  32. 1. Get a section of memory from the heap.
  33. 2. Use an array reference to correspond to a memory location on the
  34. heap.
  35.  
  36. Of course, this implies that variable-sized arrays can be created and
  37. referenced from the heap.
  38.  
  39. ie. 
  40.  
  41. main()
  42. {
  43.   int *Array;
  44.   int size;
  45.  
  46.   /* define size of the array: can be acquired from user, etc.
  47.      Point is, it is variable, not a constant. */
  48.  
  49.   Array = (int *) malloc (sizeof (int) * size);
  50.  
  51.   /* reference 5th element of array */
  52.   Array[5] = 100;
  53. }
  54.  
  55. This works because C considers Array[5] the same as pointer to Array
  56. plus 5 * the size of each element (ie. adjusts the location of the
  57. pointer).
  58.  
  59. As you can see, this is an extremely powerful approach to
  60. dynamically-sized arrays without having to fiddle with pointers too
  61. much.
  62.  
  63. Now, I tried doing something like this in Pascal, but had no luck. Any
  64. suggestions?
  65.  
  66. Gerald.
  67. gboersma@sce.carleton.ca
  68.  
  69.  
  70.