473,466 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

const against non const pointer

Hello,

if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?

matti

Nov 7 '06 #1
4 1403

mat...@gmx.at wrote:
Hello,

if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?
You have it right.

Nov 7 '06 #2
ma****@gmx.at wrote:
if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?
See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-18.5

Cheers! --M

Nov 7 '06 #3
ma****@gmx.at wrote:
if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?
The first variant means that the integer the pointer points to cannot be changed
through pointer 'test' without an explicit cast. Note that it is still possible
that the integer can be changed through some other access path (directly or
through some other pointer). Also note that if the integer itself is not a
constant object, it is perfectly legal to cast away the constness an change it
through 'test':

*const_cast<int*>(test) = 42;

The second variant means that pointer 'test' cannot be changed (as I understand,
that what you meant by "the location to which the pointer points to cannot be
changed").

--
Best regards,
Andrey Tarasevich
Nov 7 '06 #4
ma****@gmx.at wrote:
Hello,

if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?

matti
Others have already confirmed that you are correct. As a tip, these
sorts of declarations, notably ones with brackets or parentheses, can in
general be read right to left:

1) test is a pointer to an int constant
2) test is a constant pointer to an int

For more complicated declarations, check out:

http://www.ericgiguere.com/articles/...larations.html
(it's a bit old and written for C, but the basic ideas still hold)

Finally, there's a handy little UNIX utility that will convert
declarations to english and vice versa. Look for cdecl or c++decl.
Nov 7 '06 #5

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

Similar topics

4
by: Pelle Beckman | last post by:
Hi, I saw this code in an earlier post (not that there's anything wrong with it) #include <iostream> using std::cout; const int hour = 3600; const int min = 60;
83
by: user | last post by:
Hello, Here is the program #include stdio int main(void) { const int num = 100; int *ip;
1
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass =...
3
by: System Administrator | last post by:
I have a function int match(struct regexp *regexp, char *string, char **tail) which matches a string against a regular expression, and if it succeeds sets *tail to be the unmatched tail of the...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
4
by: Ben Petering | last post by:
Hi group, this is a 'best practice' type question (I want discussion of the issue - whys and why nots - similar to "casting the return value of malloc()", to cite an analogous case). Let's...
6
by: Immortal_Nephi | last post by:
This Run() function is function pointer. It contains three functions. How can you put const in Run() function? It should guard against modifying Run()'s function pointer array. Do you know what...
13
by: Adem | last post by:
HOWTO: const and pointer variants in C and C++ : void test() { int n = 1; // n is non-const data of type int ++n; // ok const int c = 2; //...
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
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.