home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: comp.lang.c
- Subject: Re: const and pointers in ANSI C
- Date: 12 Jan 1993 21:14:15 GMT
- Organization: Lawrence Berkeley Laboratory, Berkeley CA
- Lines: 51
- Message-ID: <28371@dog.ee.lbl.gov>
- References: <1993Jan12.164949.15684@clsi.COM>
- NNTP-Posting-Host: 128.3.112.15
-
- In article <1993Jan12.164949.15684@clsi.COM> shankha@clsi.COM
- (Shankha Mitra) writes:
- > typedef int *int_pointer;
- > const int_pointer x;
- > int_pointer const y;
-
- [In ANSI C, x and y wind up with the same type, despite the fact that if
- the typedef is expanded as if it were a #define, they would wind up with
- different types.]
-
- >My questions:
- >1) Why this disparity in behavior between using explicit types (the first
- > code fragment) and typedefed types (second code fragment)?
-
- For the same reason that, e.g.,
-
- typedef int *iptr_t;
- iptr_t a, b;
-
- declares two pointers, rather than one pointer and one `int'. Typedefs
- are not like #define's. Although typedef'd names are not *new* types---
- they are simply alternative names for *existing* types---they must be
- treated as atomic entities. Hence, `const' cannot `penetrate' a typedef.
-
- Consider, e.g.,
-
- typedef double value_t;
- const value_t x = X_INIT;
- value_t const y = Y_INIT;
-
- In this case, x and y are obviously both intended to be read-only.
- Suppose we later decide to change the representation of a value_t
- so that we can share values:
-
- typedef struct vblock {
- int refcount;
- double val;
- } *value_t;
-
- Because `const' does not penetrate typedefs, x and y remain `const'.
- If it did penetrate typedefs, suddenly x would be read/write, pointing
- to a read-only `struct vblock', while y would remain read-only and
- would point to a read/write `struct vblock'.
-
- >2) Is it possible to make a declaration of a pointer to constant data
- > using the typdefed int_pointer?
-
- No.
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-