explanation needed on const pointers

D

deepunayak

I need complete explanation on constant pointers.

e.g

char *const ptr ;
const char *ptr ;
char *const* ptr;

please explain me the differences.
 
T

tatto0_2000

I need complete explanation on constant pointers.

e.g

char *const ptr ;
const char *ptr ;
char *const* ptr;

please explain me the differences.

Is this ==== char *const* ptr; ====== declaration valid ??
 
P

Pedro Graca

I need complete explanation on constant pointers.

e.g

char *const ptr ;

ptr = NULL; /* not allowed */
ptr[0] = 42; /* allowed */
const char *ptr ;

ptr = NULL; /* allowed */
ptr[0] = 42; /* not allowed */
char *const* ptr;

ptr = NULL; /* allowed */
ptr[0] = NULL; /* not allowed */
ptr[0][0] = 42; /* allowed */
 
B

Ben Bacarisse

I need complete explanation on constant pointers.

Ah, you'll need a good book for that.
char *const ptr ;
const char *ptr ;
char *const* ptr;

please explain me the differences.

If you use the rule I outlined in:

http://groups.google.co.uk/group/co...9f1cd41f?lnk=st&rnum=1&hl=en#e44e3d249f1cd41f

you can read these as English words which will help to get you started.

<OT>What is the best way to reference a previous posting? The above
looks horrible and I can't from the style is the URI in long-lived or
not.</OT>
 
S

Seemanta Dutta

value pointed by ptr is constant.
ptr can be changed.
value pointed by ptr can change.
ptr itself can't change. neither ptr, nor the value pointed by ptr can change.
Is this ==== char *const* ptr; ====== declaration valid ??
Yes, see above.

By the way, did you try looking it up in some book before posting it
here?

~seemanta
 
A

Antonio Contreras

I think you need to study some more C before giving advice.
value pointed by ptr is constant.
ptr can be changed.

Just the other way arround. Here ptr is a constant pointer to a char,
which means that the char it points to can be changed, but ptr itself
cannot be changed.
value pointed by ptr can change.
ptr itself can't change.

Again, the other way arround. Here ptr is a pointer to a constant char,
which means that ptr can be changed but you cannot change the char it
points to through this variable.
neither ptr, nor the value pointed by ptr can change.

Completely wrong. Here ptr is a pointer to a constant pointer to a
char. This means that ptr can be changed. The pointer it points to
cannot be changed, but the char pointed by the pointer pointed to by
ptr can be changed.
Yes, see above.

By the way, did you try looking it up in some book before posting it
here?

Did you try to looking it up before posting advice that was incorrect?
 
J

John Bode

I need complete explanation on constant pointers.

e.g

char *const ptr ;

ptr may not be modified; *ptr may be modified:

*ptr = 'b'; /* allowed */
ptr = NULL; /* not allowed */
const char *ptr ;

ptr may be modified; *ptr may not be modified:

*ptr = 'b'; /* not allowed */
ptr = NULL; /* allowed */
char *const* ptr;

ptr may be modified; *ptr may not be modified; **ptr may be modified:

ptr = NULL; /* allowed */
*ptr = NULL; /* not allowed */
**ptr = 'a'; /* allowed */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top