home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!uwm.edu!ogicse!flop.ENGR.ORST.EDU!gaia.ucs.orst.edu!umn.edu!noc.msc.net!news.stolaf.edu!guenther
- From: guenther@stolaf.edu (Philip Guenther)
- Newsgroups: comp.lang.c
- Subject: Re: const and pointers in ANSI C
- Message-ID: <GUENTHER.93Jan12150207@snookles.stolaf.edu>
- Date: 12 Jan 93 21:02:27 GMT
- Article-I.D.: snookles.GUENTHER.93Jan12150207
- References: <1993Jan12.164949.15684@clsi.COM>
- Sender: news@news.stolaf.edu
- Organization: Academic Computing Center, St. Olaf College
- Lines: 65
- In-Reply-To: shankha@clsi.COM's message of Tue, 12 Jan 93 16:49:49 GMT
-
- In article <1993Jan12.164949.15684@clsi.COM> shankha@clsi.COM (Shankha Mitra) writes:
- 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?
-
- How about the differences between the following:
- const int i;
- int const i;
- There are none! Your problem is that you see a typedef as a pretty
- macro. It isn't. When you say 'typedef int *int_pointer', you have
- created a new type. (As a gross over simplification) when you declare
- a variable of a given type, you reserve a memory area big enough to
- hold that type *without interpreting what the type 'means'*. Thus the
- fact that you declared y to be "const int_pointer" says that y must be
- large enough to hold an "int_pointer" (whatever that is!) and that it
- can't be modified.
-
- My questions:
- 1) Why this disparity in behavior between using explicit
- types (the first code fragment) and typedefed types
- (second code fragment)?
-
- In the one, the const is either part of the type or a type modifier
- while in the other (the typedef case), the const is only a type
- modifier.
-
- 2) Is it possible to make a declaration of a pointer to
- constant data using the typdefed int_pointer? If so, how?
-
- No. This would require the compiler to take apart the typedefed type
- to determine how to apply the const. This isn't hard, but it isn't
- desireable. You can already do it with macros and it would break the
- concept of the typedef. Consider what would happen if you later had
- to change int_pointer to be a struct or a pointer to a pointer, or...
-
- Shankha
-
- Philip Guenther
- --
- guenther@stolaf.edu (Philip Guenther) | The ACC might agree with me,
- Student Sys Prog, Academic Computing Center | but with that bunch, (and me)
- St Olaf College, Northfield, MN 55057 | you never know... :-| :-( :-)
- "Life makes sense? LIFE MAKES SENSE!?!? Where do people get these ideas?"
-