Recently, I ran into an interesting job interview question in C++ which I want to share. Lets take a look at the following code:
// Option A. char str1[] = “example”; str1[1] = ‘a’; // Option B. char* str2 = “example”; str2[1] = ‘a’;
What would happen if we compile and run this code ?
- This code doesn’t compile.
- This code compiles but option A will produce a run time error.
- This code compiles but option B will produce a run time error.
- This code compiles and runs successfully.
Take your time and think about it…

Before answering this question, lets first understand this code and discuss both options:
Option A:
Array of 8 characters (’\0′ is added) is allocated on the stack.
Option B:
An unnamed, static array of characters is stored in the read only data segment. Str2 is a pointer that is allocated on the stack, but it points to the read only array’s first character.
So, when we try to change the data in the first option, every thing is fine and str1 becomes “eaample”. But when trying to change the data that is pointed by str2 (read only data), “Access violation writing location …” exception is raised.
The correct answer is: 3. This code compiles but option B will produce a run time error.
I must point out that some compilers have the option -fwritable-strings which makes the constant strings non-constant, but this is for compilation of old code.
So, what do you have to say? Do you have more interesting “job interview” questions? Would you like to see a weekly “Job Interview Challenge” in Dev102?

















By Jordan on Apr 28, 2008 | Reply
This was a good question. I have to admit I had to run it myself just to make sure. With the prevalence of STL strings, MFC’s CString and others, I can’t say I’ve used good old C-style strings in quite some time.
By commenter on Apr 28, 2008 | Reply
The correct answer is “Uhuh. See, this is one of the many reasons why you shouldn’t use C++”.
By Bob on Apr 28, 2008 | Reply
The great thing about software is that you can write it and click compile, then run and find the error.
By gigi on Apr 28, 2008 | Reply
Without specifying the compiler, the version and the compiler options this question can’t be answered
By signal9 on Apr 28, 2008 | Reply
WOW Commenter, your clever. Can we delete this Trolls comment please.
This was a good question for an interview. Its simple and ensures the new hire understands certain concepts.
By Shahar Y on Apr 28, 2008 | Reply
Hi gigi,
As i pointed out in the post, some compilers have the option -fwritable-strings which makes the constant strings non-constant (for compilation of old code).
As far as i know, the behaviour will be the same in all other cases.
Please, correct me if i am wrong.
By Michael on Apr 28, 2008 | Reply
Dear gigi,
IMHO Shahar is correct. This is well known and well defined - please read Stroustrup 3rd edition chapter 5.5.
enjoy
By Jason on Apr 28, 2008 | Reply
This is a great question like Backstreet Boys are great songwriters…
Here’s another great question that tests whether candidates know C++ welll
for(int i = 0; i < 100; ++i);
printf(”X”);
How many X’s appear?
Great question, I’m really clever
By OJ on Apr 28, 2008 | Reply
Commenter: You’re incorrect (and ignorant). Stick to managed code and languages which require you not to think — it’s better for all of us.
Everyone else with a brain: Great question, one that I’ve faced before in an interview where they did specify compiler and version
By CPlusPlusGuru on Apr 29, 2008 | Reply
Jason: The answer is 1
Great question because it makes me feel smart
By Dave Regan on Apr 30, 2008 | Reply
Seems to me like the code will not compile. It should come up with a “str1 is undefined” compile error no? Since the definition of str1 is commented out:
// Option A.char str1[] = “example”;
By Shahar Y on Apr 30, 2008 | Reply
Dave Regan,
This was not my intention, of course.
Thanks for pointing the typing mistake out.
It is fixed now…
By scoopr on May 14, 2008 | Reply
Any decently modern compiler would warn about casting string literal to char* instead of const char*,
foo.cpp:4: warning: deprecated conversion from string constant to ‘char*’
So, it would not compile with warnings as errors switch, so I insist the code is badly formed, even if it might compile.