home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:11815 comp.lang.pascal:4648
- Newsgroups: comp.lang.c,comp.lang.pascal
- Path: sparky!uunet!email!hp
- From: hp@vmars.tuwien.ac.at (Peter Holzer)
- Subject: Re: Flexible array indexing in C (like Pascal)
- Message-ID: <1992Jul31.154404.13833@email.tuwien.ac.at>
- Sender: news@email.tuwien.ac.at
- Nntp-Posting-Host: quasi.vmars.tuwien.ac.at
- Organization: Technical University Vienna, Dept. for Realtime Systems, AUSTRIA
- References: <1992Jul31.140623.9280@utdallas.edu>
- Date: Fri, 31 Jul 1992 15:44:04 GMT
- Lines: 26
-
- veerasam@utdallas.edu (Jeyakesavan Veerasamy) writes:
-
- >When translating Pascal programs to C, this tip may be helpful.
-
- >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)
-
- Note that this only is garantueed to work if low <= 0 <= high + 1. If
- this condition is not satisfied, the returned pointer does not point
- into the allocated memory region, and may cause an exception when used.
-
- [This `tip' comes up every few months. I wish people would read the FAQ
- (Q. 2.11 in this case) before posting their pet tricks]
-
- Peter
- --
- | _ | Peter J. Holzer | Think of it |
- | |_|_) | Technical University Vienna | as evolution |
- | | | | Dept. for Real-Time Systems | in action! |
- | __/ | hp@vmars.tuwien.ac.at | Tony Rand |
-