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 ?
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?
We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn More
Jordan Said on Apr 28, 2008 :
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.
commenter Said on Apr 28, 2008 :
The correct answer is “Uhuh. See, this is one of the many reasons why you shouldn’t use C++”.
Bob Said on Apr 28, 2008 :
The great thing about software is that you can write it and click compile, then run and find the error.
gigi Said on Apr 28, 2008 :
Without specifying the compiler, the version and the compiler options this question can’t be answered
signal9 Said on Apr 28, 2008 :
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.
Shahar Y Said on Apr 28, 2008 :
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.
Michael Said on Apr 28, 2008 :
Dear gigi,
IMHO Shahar is correct. This is well known and well defined - please read Stroustrup 3rd edition chapter 5.5.
enjoy
Jason Said on Apr 28, 2008 :
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
OJ Said on Apr 28, 2008 :
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
CPlusPlusGuru Said on Apr 29, 2008 :
Jason: The answer is 1
Great question because it makes me feel smart
Dave Regan Said on Apr 30, 2008 :
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”;
Shahar Y Said on Apr 30, 2008 :
Dave Regan,
This was not my intention, of course.
Thanks for pointing the typing mistake out.
It is fixed now…
scoopr Said on May 14, 2008 :
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.
Albus Brian Said on May 26, 2008 :
hi it’s my first time to view this site,I’m an I.T student.Can someone tell me if there is a C# compiler available in any website?? thanx
hi to all!!
Shahar Y Said on May 26, 2008 :
Hi Brian
I would recommend using the Visual Studio Express Edition.
It is free and you can download it here:
http://www.microsoft.com/express/download/
Good luck
Angel Said on May 27, 2008 :
@Jason, it will print 100 X… some time later, as output will be cached by stdio (unless you disabled it for stdout with setvbuf).
So it’ll be output when on next line (if output is a terminal), the buffer fulls (but it’s larger than 100 bytes) or at the end of your program. I don’t think it’ll print if you abort() though, implementation defined anyway.
Adam Said on Jun 6, 2008 :
@Angel
Thats actually incorrect. It will only print out 1 “X”, because of the semi colon after the for statement that it will loop, and once it finishes print out the X.
Talk Binary Said on Aug 19, 2008 :
I had a friend who had relatively about the same format of questions. She had questions on C++ using heaps, linked list, and such.
She said they felt more like “trick” questions.