Dear members,

           Am learning C++ by Yashawant Kanetkar from his book "Let us C++", its been said his books are good for beginners who do self study like me, but I find some conflict of logic in this book, please help me clear those...

The book says......the following code fragment would help fix ideas about const further

char *p = "Hello" ; //pointer is variable , as well string is <<< I find this wrong
*p = 'M' ; // error
p = "Bye"; //works

const char *q = "Hello"; //pointer is variable, string is constant <<<<I agree, pls confirm if its right
*q = 'M' ; // error
q = "Bye" ; //works

char const *s = "Hello" ; //pointer is variable, string is constant <<<I agree, pls confirm if its right
*s = 'M' ; // error
s = "Bye" ; // works

char * const t = "Hello"; //pointer is constant, so is string<<<<I find this wrong
*t = 'M' ; //works
t= "Bye" ; //error

const char * const u = "Hello"; //string is fixed, so is pointer <<< I agree, pls confirm..
*u = 'M' ; // error
u ="Bye" ; // error

char *p = "Hello" ; // The pointer is mutable . "Hello" is a string literal and as per c++ standard ,you
//are not supposed to modify it . Doing so may result in unexpected behavior.
*p = 'M' ; // Do not use the pointer to change .
p = "Bye"; //works

const char *q = "Hello"; //pointer is mutable and points to a immutable .
char const *s = "Hello" ; //pointer is mutable and points to a immutable .hts
char * const t = "Hello"; //pointer is immutable and points to a mutable.
const char * const u = "Hello"; // immutable pointer to a immutable.

I would recommend C++ Primer Plus by stephen prata .

Dear ravi_14

      Thanks a lot for your response, I value the time taken by you to help me out, please help me understand this "When you say pointer do you mean *q or just the variable q ????

Edited 1 Year Ago by surfingturtle

A pointer is simply a variable that holds an address.

// compiler told val is a label to access this int '10' it puts into memory somewhere //
int val = 10; 

cout << "val = " << val << endl; //compiler knows val is an int and block address in memory//

// p is a label to access the address of some int
// compiler informed that p is to hold address of an int //
int* p = &val; // p now holds the address of val //

cout << "*p = " << *p << " and p = " << p << endl;

If you compile and run the above simple example ... it may help.

Note that every variable name is an alias (a label ... a handle) to some address ... thus you can take the address of a pointer variable and so have a pointer to a pointer

int** pp = & p;

Edited 1 Year Ago by David W

P.S.
If you lookup direct addressing vs. indirect addressing ...
that too may help you understand more about pointers.

If you also lookup
read-only memory vs. read and write memory
that too should shed some light on what the compiler is doing.

Your question, perhaps, could be re-expresed like this:
what is the correct C/C++ syntax to say a value is held constant (after it is first created)
and ...
what is the correct C/C++ syntax to say an address is held constant (after it is first created)

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.