home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!clsi!shankha
- From: shankha@clsi.COM (Shankha Mitra)
- Subject: const and pointers in ANSI C
- Message-ID: <1993Jan12.164949.15684@clsi.COM>
- Originator: shankha@clsi
- Sender: usenet@clsi.COM
- Organization: CAD Language Systems, Inc.
- Date: Tue, 12 Jan 93 16:49:49 GMT
- Lines: 45
-
-
-
- Consider the following C code:
-
- int * const constant_pointer;
- const int *pointer_to_constant_data;
-
- So the location of the type qualifier const in the declaration determines whether
- the pointer itself or the contents is the constant entity.
-
- Now suppose you use this instead:
-
- typedef int *int_pointer;
- const int_pointer x;
- int_pointer const y;
-
- My compiler (xlc on the RS/6000) which is fully ANSI compatible (or so they claim)
- considers the declarations of x and y to be equivalent; it considers x and y to
- be constant pointers. That is I can say,
-
- *x = *y = 1;
-
- but not
-
- x = y = &i;
-
- Interesting, huh?
-
- The "C Reference Manual" (Harbison and Steele) corroborates the above behavior.
- So does gcc -ansi.
-
- My questions:
- 1) Why this disparity in behavior between using explicit types (the first
- code fragment) and typedefed types (second code fragment)?
- 2) Is it possible to make a declaration of a pointer to constant data
- using the typdefed int_pointer? If so, how?
-
- Thanks in advance.
-
- Shankha
- --
- -------------------------------------------------------------------------------
- Shankha S. Mitra shankha@clsi.com
- CAD Language Systems, Inc. (410) 992-1716
- -------------------------------------------------------------------------------
-