home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.pascal
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!nott!cunews!genesis.sce.carleton.ca!gboersma
- From: gboersma@sce.carleton.ca (Gerald Boersma)
- Subject: Re: pointers to arrays?!?
- Message-ID: <gboersma.726780493@genesis.sce.carleton.ca>
- Sender: news@cunews.carleton.ca (News Administrator)
- Organization: Carleton University
- References: <1993Jan11.205412.1@cc.newcastle.edu.au> <1is7qmEINNfgi@uni-erlangen.de>
- Date: Mon, 11 Jan 1993 19:28:13 GMT
- Lines: 60
-
- gedeck@pctc.chemie.uni-erlangen.de (Peter Gedeck) writes:
-
-
- >|>Type
- >|> ArrayType = Array[1..Max] of Char;
- >|>
- >|>Var
- >|> ArrayVar : ^ArrayType;
- >|> Index : Word;
- >|>
- >|>[..]
- >|>
- >|>ArrayVar[Index]^ := etc..????
-
- >ArrayVar^[Index] := etc..
- >
- >Peter Gedeck
-
- In C, it is possible to do the following with arrays on the heap:
-
- 1. Get a section of memory from the heap.
- 2. Use an array reference to correspond to a memory location on the
- heap.
-
- Of course, this implies that variable-sized arrays can be created and
- referenced from the heap.
-
- ie.
-
- main()
- {
- int *Array;
- int size;
-
- /* define size of the array: can be acquired from user, etc.
- Point is, it is variable, not a constant. */
-
- Array = (int *) malloc (sizeof (int) * size);
-
- /* reference 5th element of array */
- Array[5] = 100;
- }
-
- This works because C considers Array[5] the same as pointer to Array
- plus 5 * the size of each element (ie. adjusts the location of the
- pointer).
-
- As you can see, this is an extremely powerful approach to
- dynamically-sized arrays without having to fiddle with pointers too
- much.
-
- Now, I tried doing something like this in Pascal, but had no luck. Any
- suggestions?
-
- Gerald.
- gboersma@sce.carleton.ca
-
-
-