473,508 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

explanation needed on const pointers

I need complete explanation on constant pointers.

e.g

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

please explain me the differences.

Feb 16 '06 #1
9 2624
Hi,
You can find out something regarding the const pointer Useage at
following URL:

http://www.geocities.com/spur4444/pr..._pointers.html
de********@gmail.com wrote:
I need complete explanation on constant pointers.

e.g

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

please explain me the differences.


Feb 16 '06 #2

de********@gmail.com 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 ??

Feb 16 '06 #3
de********@gmail.com 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>
Feb 16 '06 #4
On Wed, 15 Feb 2006 23:20:17 -0800, de********@gmail.com 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.

Feb 16 '06 #5
ta*********@yahoo.com wrote:
de********@gmail.com 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
Feb 16 '06 #6
Seemanta Dutta wrote:
ta*********@yahoo.com wrote:
de********@gmail.com 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?

Feb 16 '06 #7
<de********@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro 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
Feb 16 '06 #8

de********@gmail.com 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.


Feb 16 '06 #9
In article <11*********************@g43g2000cwa.googlegroups. com>,
"de********@gmail.com" <de********@gmail.com> 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.
Feb 16 '06 #10

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

Similar topics

5
5047
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
20
2463
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
19
6805
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
2
3620
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
6
1986
by: R.Z. | last post by:
i'm using a class from some api that is said to automatically call its destructor when its out of scope and deallocate memory. i create instances of this class using "new" operator. do i have to...
9
2119
by: Alex | last post by:
Hi. I'll try my problem with this example: class C { protected: virtual int* getProtected(int index)=0; public: const int* get(int index) const { return (const int*) getProtected(index); } ...
4
2323
by: Yogesh | last post by:
Hi List, I am overloading new and delete globally for memory leak detection. void* operator new(std::size_t s, const char * file , int inLine) ; ->case1 void* operator new(std::size_t s, const...
5
2272
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
17
2412
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
0
7231
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
7132
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
7401
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
7504
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
5640
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
4720
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
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.