home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!sun-barr!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: comp.lang.c
- Subject: Re: Array question....
- Date: 26 Jul 1992 01:50:57 GMT
- Organization: Lawrence Berkeley Laboratory, Berkeley
- Lines: 58
- Distribution: ca
- Message-ID: <24900@dog.ee.lbl.gov>
- References: <14ks9uINNgmj@agate.berkeley.edu> <22JUL199218153390@envmsa.eas.asu.edu>
- Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
- NNTP-Posting-Host: 128.3.112.15
- Keywords: help?
-
- >In article <14ks9uINNgmj@agate.berkeley.edu>,
- >mdr@ocf.berkeley.edu (Mike Rogoff) writes...
- >>... If you just malloc a bunch of space, can you reference it with
- >>array [] subscripts and have it work?
-
- It is not *quite* like that: malloc returns a value, and you have to
- store that value in some object in order to use it more than once.
- The type of this object determines what operations are legal, and the
- size you pass to malloc figures into how many subscripts, if any, are
- legal. But this is essentially correct.
-
- char *p;
-
- p = malloc(300);
-
- If p is not set to NULL, then p[0] through p[299] are all legal, and
- all produce an object of type `char'.
-
- int *q;
-
- q = malloc(10 * sizeof *q);
-
- If q is not set to NULL, q[0] through q[9] are all legal, and all
- produce an object of type `int'.
-
- For more information, see the FAQ.
-
- In article <22JUL199218153390@envmsa.eas.asu.edu>
- ptran@envmsa.eas.asu.edu (Phi-Long Tran) writes:
- > You want to declare a pointer to an array:
- >
- > MyType (*paryMyType)[MAX_ELEMENTQ];
-
- Probably not. A pointer to an array can point to zero or more
- contiguous arrays, so that, e.g., you could write:
-
- paryMyType = malloc(6 * sizeof *paryMyType);
-
- Now if paryMyType is not NULL, p[0] through p[5] are all legal, and
- all produce an object of type `array MAX_ELEMENTQ of MyType'. Each
- of those array objects can in turn be subscripted, so if MAX_ELEMENTQ
- is at least 9, then
-
- paryMyType[3][4]
-
- is legal and is an object of type `MyType'.
-
- If you only wanted a single linear set of `MyType's, it would be
- simpler to write
-
- MyType *m;
-
- m = malloc(33 * sizeof *m);
-
- Now if m is not NULL, m[0] through m[32] are all legal, etc.
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-