473,414 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Pointers, typedef's and const's

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
Jun 27 '08 #1
7 1945
On May 1, 5:00 pm, Alex <yakov...@hotmail.comwrote:
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
<http://c-faq.com/Question 11.11, <http://c-faq.com/ansi/
typedefconst.html>.

--
Robert Gamble

Jun 27 '08 #2
Alex wrote:
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?
Why not???

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #3
On May 1, 5:04 pm, Robert Gamble <rgambl...@gmail.comwrote:
On May 1, 5:00 pm, Alex <yakov...@hotmail.comwrote:
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

<http://c-faq.com/Question 11.11, <http://c-faq.com/ansi/
typedefconst.html>.

--
Robert Gamble
Robert, thanks

From the FAQ 11.11: "The typedef'ed declaration of p does not ``look
inside'' the typedef to see that there is a pointer involved."

So meanings of "const POINTER" and "POINTER const" are the same... It
looks counterintuitive to me.

Do you know any reason, why it has been done this way? Was it too
difficult to look inside the typedef? I don't think so - pointer
dereferencing works, right?

Best wishes,
Alex
Jun 27 '08 #4
Alex wrote:
>
From the FAQ 11.11: "The typedef'ed declaration of p does not ``look
inside'' the typedef to see that there is a pointer involved."

So meanings of "const POINTER" and "POINTER const" are the same... It
looks counterintuitive to me.
It looks counterintuitive to you probably because you think of
typedef-names as macros, while in reality they are not macros, but
alternative names for types.

Meaning of "const POINTER" and "POINTER const" is the same just like the
meaning of "const int" is the same as that of "int const".

The current behavior qualifiers applied to typedef-names is perfectly
intuitive, once you get the proper understanding of what typedef-names are.
Do you know any reason, why it has been done this way? Was it too
difficult to look inside the typedef?
The real question in this case is why would anyone even want to "look
inside the typedef" as you suggest.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #5
On Thu, 1 May 2008 14:00:46 -0700 (PDT), Alex <ya******@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.c http://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
Jun 27 '08 #6
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
Jun 27 '08 #7
On May 1, 4:00 pm, Alex <yakov...@hotmail.comwrote:
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
Because you've created a synonym for a pointer type, and you're
applying the const qualifier to the synonym; i.e., you've said you
want the pointer value to be constant, not the thing being pointed
to.
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
8
by: Amit | last post by:
Hello all. If I want to use an object both as a Functor and also, if I pass a function pointer, how can it be done ? For instance, I have something like this template< typename Iter, typename...
7
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop...
0
by: Michael | last post by:
Hi developers. I´m trying to migrate a C++ sample from the Canon SDK Version 7.1 into C#. I have come across lots of pit falls (and luckily solved them) but now I´m stuck with some of the nice...
10
by: danibe | last post by:
I never had any problems storing pointers in STL containers such std::vector and std::map. The benefit of storing pointers instead of the objects themselves is mainly saving memory resources and...
6
by: Martin | last post by:
Hi I need to maintain a <setof pointers to objects, and it must be sorted based on the values of pointed objects, not pointer values. I can achieve this easily by defining my own comparing...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
3
by: Thomas Nelson | last post by:
I'm trying to write a function that uses function pointers. Here's my errors: Board.cc:111: error: expected ',' or '...' before 'p1' Board.cc:111: error: prototype for 'int Board::play_game(int...
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.