On May 1, 10:17 pm, Jack Klein <jackkl...@spamcop.netwrote:
On Thu, 1 May 2008 14:00:46 -0700 (PDT), Alex <yakov...@hotmail.com>
wrote in comp.lang.c:
The code below can't be compiled:
typedef char* POINTER;
const POINTER ptr;
ptr++;
The compiler (Sun C 5.8 Patch 121015-04 2007/01/10) complains:
"test.c", line ...: operand must be modifiable lvalue: op "++"
It looks like the "const POINTER" has been compiled to "char* const".
A question is - why?
Best wishes,
Alex
That is just one of the reasons why hiding a pointer in a typedef is a
mistake in 99% of the cases where you see it.
Why do you want to use a typedef instead of using "char *"? What do
you think that you gain?
--
Jack Klein
Home:http://JK-Technology.Com
FAQs for
comp.lang.chttp://c-faq.com/
comp.lang.c++http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Well, I don't use a typedef instead of "char *". I just was annoyed by
chains of asterisks, caused by "pointer to pointer to pointer"
declarations and tried to improve my code readability.
I tend to consider typedef's as user-defined types - that's why I
never write "char *ch", but "char* ch" instead (and I don't declare
many variables in one statement). I expected my new types to behave
like predefined types, including a possibility to "constanize" them.
May be, this two-face functionality of "const" for pointers is a real
evil here. I'd prefer to write something like this:
(const char)* const ptr;
But it's not C - right? I like the "const" keyword and use it A LOT,
so one more heretical thought - all the variables should be "const" by
default, and if you need a non-const variable, you'll have to declare
that fact with some keyword.
Best wishes,
Alex