Jump to content
  • Advertisement
Sign in to follow this  
musafir2007

differnce between const char x and char const x

This topic is 2518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!