473,405 Members | 2,310 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,405 software developers and data experts.

Question about declaring const pointer to const value

Hi,
I have the following code:

int main(){ /* line 1 */
const int i = 10; /* line 2 */
const int j = 20; /* line 3 */
/* line 4 */
int const * const p = &i; /* line 5 */
const int * const p2 = &j; /* line 6 */
/* line 7 */
(*p2)++; /* line 8 */
(*p)++; /* line 9 */

return 0;
}

Compiler does not complain line 6 and errors out on those
incremental statements of line 8 and 9.

So the declaration on line 5 and line 6 are the same? I mean "int
const * const" and "const int * const" are same?

It is a little hard to grasp how to use "const" in declaration.

Any tip?

Sep 18 '07 #1
2 1265
linq936 <li*****@gmail.comwrote:
So the declaration on line 5 and line 6 are the same? I mean "int
const * const" and "const int * const" are same?

It is a little hard to grasp how to use "const" in declaration.

Any tip?
Yes. Read the friggin' FAQ already.

<http://c-faq.com/ansi/constptrconst.html>

Richard
Sep 18 '07 #2
linq936 wrote:
>
I have the following code:

int main(){ /* line 1 */
const int i = 10; /* line 2 */
const int j = 20; /* line 3 */
/* line 4 */
int const * const p = &i; /* line 5 */
const int * const p2 = &j; /* line 6 */
/* line 7 */
(*p2)++; /* line 8 */
(*p)++; /* line 9 */

return 0;
}

Compiler does not complain line 6 and errors out on those
incremental statements of line 8 and 9.

So the declaration on line 5 and line 6 are the same? I mean "int
const * const" and "const int * const" are same?

It is a little hard to grasp how to use "const" in declaration.
The thing that counts is which side of the * the 'const' is.
"const int *p" means p is a non-constant pointer which points at
read-only int(s). "int * const p = &x;" means p is a read-only
pointer, set to point at the variable x, and which can only be set
at this declaration point.

Try to use fixed width fonts, so as to avoid the silly widths.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 18 '07 #3

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

Similar topics

18
by: Jason Heyes | last post by:
Is this class evidence of poor design? class Rectangle { double width, height; public: double get_width() const { return width; } double get_height() const { return height; } void...
15
by: natespamacct | last post by:
Hi All, I'm not sure if I'm dealing with a C++ question or a compiler question, so please forgive me if I'm asking in the wrong spot. If so, maybe someone can direct me to more appropriate spot....
83
by: user | last post by:
Hello, Here is the program #include stdio int main(void) { const int num = 100; int *ip;
8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
9
by: vashwath | last post by:
Hi all, Recently I attended an interview in which the question "Is there any difference between "const T var" and "T const var"? was asked.I answered "NO"(I guessed it:-( ).Did I answered...
1
by: Bern McCarty | last post by:
I am using MEC++ in VC 7.1. I had a method on a __gc object that looked like this: __property System::UInt32 get_MyProperty(void) { System::Byte __pin * pinBytes = &m_byteArray; // entire...
5
by: John | last post by:
I thought I had a fairly good understanding of typedefs until I came across the following passage from C++ Primer, fourth edition by Stanley B. Lippman: "The mistake is in thinking of a typedef...
1
by: Ilya N. Golubev | last post by:
There is already one function processing pointer values this way. And there may easily appear more, including wrappers for this function. For purposes of further discussion, consider simplified...
3
by: Kush | last post by:
Hello All, Once again another question. :) This time I would like to understand why one needs to overload the operator to access objects in array format? Example code: class A {
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.