Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

I'm scratching my head over this silly one: Why does method 1 give me an error, and method 2 compile just fine? (don't mind the warning though( 1. http://ideone.com/c3aju 2. http://ideone.com/MuEpG

OpenStudy (anonymous):

Btw, in method 2. if I switch the char* and the const keywords around, it gives me an error.

OpenStudy (anonymous):

http://ideone.com/jMPPm

OpenStudy (shadowfiend):

It's the difference between making the variable constant and the pointer constant. const char* makes the pointer constant, meaning you won't be able to modify its contents. char* const, which is what the typedefed version also boils down to, makes the variable constant, meaning you can't reassign to it.

OpenStudy (anonymous):

it's all clear now :-D

OpenStudy (anonymous):

Right; http://ideone.com/GlVeW

OpenStudy (anonymous):

Maybe that K&R book can help me understand how the const keyword really works in C.

OpenStudy (anonymous):

Ah, K&R Page 40 verse 5, and Lo!: "The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed. For an array, the const qualifier says that the elements will not be altered."

OpenStudy (anonymous):

However, if I take method 1 and initialize it on the same line, it doesn't give me a compile error O.o http://ideone.com/nfkA8

OpenStudy (anonymous):

Same without the typedef http://ideone.com/pBxQ7

OpenStudy (shadowfiend):

The typedef version is expected. const protects from *reassignment*. The non-typedef one is an order thing I think (const char* vs char* const).

OpenStudy (anonymous):

Can i get a pdf format of that K & R BOOK ???????

OpenStudy (anonymous):

isn't it available for free as a pdf format file ?????

OpenStudy (anonymous):

Nope :(

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!