Advertisement

02.21.2006 at 11:48AM PST, ID: 21745743
[x]
Attachment Details

const char*  vs char* const

Asked by stevefromc in C Programming Language

Tags: char, const, vs, c

In C,

In the code below, I just want to be 100% sure that I understand the difference between statements (A) and (B):

void try_this(char *p, char *q)
{
       const char*  constptr = p;  // (A)
       char* const constptr2 = p; // (B)
}


(A) means that the data being pointed TO is constant
(B) means that the POINTER (address) is constant.

Now assuming that's the case, I could use some clarification on (A):
Suppose p was passed into the above function as "HELLO"
Suppose q was passed into the above function as "GOODBYE"

constptr  points to the beginning of "HELLO".
I cannot say *constptr = 'J' because of the nature  of the const.

But I can say
constptr = q;  // now it will point to "GOODBYE"
(is this legal???.....)

Thanks
Steve
Start Free Trial
 
Loading Advertisement...
 
[+][-]02.21.2006 at 01:18PM PST, ID: 16012909

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: char, const, vs, c
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 1
Solution Grade: A
 
 
[+][-]02.21.2006 at 01:31PM PST, ID: 16013030

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32