home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:11812 comp.lang.pascal:4644
- Newsgroups: comp.lang.c,comp.lang.pascal
- Path: sparky!uunet!usc!sdd.hp.com!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!veerasam
- From: veerasam@utdallas.edu (Jeyakesavan Veerasamy)
- Subject: Flexible array indexing in C (like Pascal)
- Message-ID: <1992Jul31.140623.9280@utdallas.edu>
- Sender: usenet@utdallas.edu
- Nntp-Posting-Host: csclass.utdallas.edu
- Organization: Univ. of Texas at Dallas
- Date: Fri, 31 Jul 1992 14:06:23 GMT
- Lines: 40
-
-
- When translating Pascal programs to C, this tip may be helpful.
-
- Flexible array indexing of pascal really gave lot of problems for me.
- (It is common to see negative array indices in scientific pascal programs).
- Following macro allocates the equavalent array in C.
-
- It basically allocates the memory, returns the pointer
- after adjusting it for index range.
-
-
- /* Allocates array for which index value low to hi will be legal,
- Only requirement is, low < hi */
-
- #define allocate(type,low,hi) \
- ((type *) malloc ( (hi-low+1) * sizeof(type) ) - low)
-
-
- /* Example usage */
- main()
- {
- char *a;
- int *b;
-
- a = allocate( char, -200, -100 );
- b = allocate( int, 'a', 'z' );
-
- a[-200] = 'A';
- b['a'] = 2;
- b['z'] = 1;
- }
-
- With regards,
- Jey.
-
- ----------------------------------------------------------------------
- | Mail: Jeyakesavan Veerasamy M.S. Computer Science |
- | 2400 Waterview Pkwy #811 The University of Texas at Dallas |
- | Richardson TX 75080-2263 Email: veerasam@utdallas.edu |
- ----------------------------------------------------------------------
-