Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > explanation needed on const pointers

Reply
Thread Tools

explanation needed on const pointers

 
 
deepunayak@gmail.com
Guest
Posts: n/a
 
      02-16-2006
I need complete explanation on constant pointers.

e.g

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

please explain me the differences.

 
Reply With Quote
 
 
 
 
Sreekanth
Guest
Posts: n/a
 
      02-16-2006
Hi,
You can find out something regarding the const pointer Useage at
following URL:

http://www.geocities.com/spur4444/pr..._pointers.html


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I need complete explanation on constant pointers.
>
> e.g
>
> char *const ptr ;
> const char *ptr ;
> char *const* ptr;
>
> please explain me the differences.


 
Reply With Quote
 
 
 
 
tatto0_2000@yahoo.com
Guest
Posts: n/a
 
      02-16-2006

(E-Mail Removed) wrote:
> 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 ??

 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      02-16-2006
(E-Mail Removed) wrote:
> 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 */

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-16-2006
On Wed, 15 Feb 2006 23:20:17 -0800, (E-Mail Removed) wrote:

> 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/com...4e3d249f1cd41f

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>

--
Ben.

 
Reply With Quote
 
Seemanta Dutta
Guest
Posts: n/a
 
      02-16-2006
(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>> I need complete explanation on constant pointers.
>>
>> e.g
>>
>> char *const ptr ;

value pointed by ptr is constant.
ptr can be changed.

>> const char *ptr ;

value pointed by ptr can change.
ptr itself can't change.
>> char *const* ptr;
>>

neither ptr, nor the value pointed by ptr can change.

>> please explain me the differences.

>
> 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
 
Reply With Quote
 
Antonio Contreras
Guest
Posts: n/a
 
      02-16-2006
Seemanta Dutta wrote:
> (E-Mail Removed) wrote:
> > (E-Mail Removed) wrote:
> >> I need complete explanation on constant pointers.
> >>


I think you need to study some more C before giving advice.

> >> e.g
> >>
> >> char *const ptr ;

> 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.

> >> const char *ptr ;

> 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.

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

> 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.

> >> please explain me the differences.

> >
> > 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?


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

 
Reply With Quote
 
stathis gotsis
Guest
Posts: n/a
 
      02-16-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> I need complete explanation on constant pointers.
>


My guesses would be the following.

> char *const ptr ;


constant pointer to char

> const char *ptr ;


pointer to constant char

> char *const* ptr;


pointer to constant pointer to char


 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      02-16-2006

(E-Mail Removed) wrote:
> 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 */
>
> please explain me the differences.


 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      02-16-2006
In article <(E-Mail Removed). com>,
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

> I need complete explanation on constant pointers.
>
> e.g
>
> char *const ptr ;
> const char *ptr ;
> char *const* ptr;
>
> please explain me the differences.
>


Any good C reference will do.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explanation needed for const int "error: variably modified ... atfile scope." Poster Matt C Programming 21 06-16-2010 05:35 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments