differnce between const char x and char const x

Started by
3 comments, last by BCullis 12 years, 3 months ago
Hi,
So i had an interview today and was asked what is the differnce between:

const char x
and
char const x

I have actually never seen "char const" so I said that. I then asked what the differnce was and the interviewer said that there is actually a big difference but didn't say what. I have searched on google but didn't find anything too helpful.
I am guessing it goes low level? how compiler behaves?

any clarification would be appreciated.
thanks
Advertisement
Who was your interviewer? They both mean the same thing, there's no difference in that case. [font=courier new,courier,monospace]const[/font] binds to the thing on it's left, but if there's nothing on it's left then it will happily bind to the thing on it's right.
So in "[font=courier new,courier,monospace]char const x[/font]" const binds to the thing on it's left, producing a "[font=courier new,courier,monospace]char+const[/font]" type. And in "[font=courier new,courier,monospace]const char x[/font]", there's nothing on the left of const, so it binds to the thing on it's right, also producing a "[font=courier new,courier,monospace]char+const[/font]" type.

If the type is a pointer, then the placement of const does have a significant impact:
A) [font=courier new,courier,monospace]const char* x;//[/font] a mutable pointer, pointing to a read-only char
B) [font=courier new,courier,monospace]char* const x;//[/font] a read-only pointer, pointing to to a mutable char
C) [font=courier new,courier,monospace]const char* const x;//[/font] a read-only pointer, pointing to a read-only char
D) [font=courier new,courier,monospace]char const* x; //[/font] same as A
E) [font=courier new,courier,monospace]char const* const x; //[/font] same as C
See also the C++ FAQ lite on this subject.
The interviewer must have been reading off a page where he forgot to put in the asterixs when he copied it from someplace. Hence the comment, ".. there is a big difference...". He must not know it himself.


If the type is a pointer, then the placement of const does have a significant impact:
A) const char* x;// a mutable pointer, pointing to a read-only char
B) char* const x;// a read-only pointer, pointing to to a mutable char
C) const char* const x;// a read-only pointer, pointing to a read-only char
D) char const* x; // same as A
E) char const* const x; // same as C


Great info Hodgman. I did not know the meaning of D and E. Now I do.
I'd agree with Marvel: if I got this question, I'd immediately ask if they meant it to include a pointer or not, as that's where it begins to really matter (as per Hodgman's clear explanation above)

Though that's a specific C "gotcha", unless the job is rooted in C (or C++) development it wasn't a very good interview question. You've either had to fudge around with pointers in C or you haven't, but that isn't necessarily going to indicate you're a better programmer. (What should have been more important is how quickly you would pick this up if you hadn't already worked with it before)

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

This topic is closed to new replies.

Advertisement