Yahoo Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
6
of 6 vote

c is a pointer to a read only location. It is in .text segment and not .bss since "abc" is a literal. Attempt to write there should crash the program.

- Anonymous March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes u are right.........same question I found in steve summit...

- saurabh March 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

char s[] = "xyz";
s[0]='p';

The above also goes in data segment(initialized one).

- but i'm able to do this November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@but: No, if you did that, "xyz" would be placed on the stack.

- eugene.yarovoi November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

For heaven's sake STOP! using TurboC.

- Shubhro March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Or the problem is its a pointer constant in line 1(char *c="abc" )which you try to move in line 2( *c='d'), not allowed in C

- Mohit March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

the program will crash cause the value of *c cannot be changed.

- Scott May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think because you did not allocate memory to the pointer.

- Mohit March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, that's not it. C will create a global string "abc" and give c a pointer to it.

- eugene.yarovoi March 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

you cant change the value, it will throw an exception.

- APM March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

C doesn't have exceptions.

- eugene.yarovoi March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can't change the value.

- Anonymous March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String literals cannot be changed in C. The behavior in this case is undefined. Your char* c is pointing to a string literal. It is a really good idea to define char* c as const char* c to catch this kind of error during compile time. Or initialize an array, rather than a pointer with the literal if you'll change it later, as in char c[10] = "abc".

- barcod March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the previous case char*c is a const char*. Lets say we change it to be char* const c, which makes it a constant pointer to a character. SO by definition you should now be able to change the value but you cant make c point to anything else right?
The below code doesnt print c either.
char* const c = "assa";
*c ="ghj";
printf("%s",c);

Also as far as I know, when you declare an array or a pointer to an array, it is basically a constant pointer to a int/char whatever. There's no reason why it shouldn't allow you to change the value being pointed to. This thing is driving me nuts. Can some explain please?

- sreemana@buffalo.edu March 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

c is a pointer to a read only location. It is in .text segment and not .bss since "abc" is a literal. Attempt to write there should crash the program.

- Guest March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It will lead to the crashing of the program ..

- Anonymous March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String literals are treated as constant strings, so, in summary you can't change them. If you run strings <binary> you'll see abc as such stored in the BSS section of the binary. So, any attempt to modify a constant memory location would result in an undefined behaviour or crash.

- skp March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My TurboC compiler gives 'dbc' as output.

- Oni March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My TurboC compiler gives 'dbc' as output.

- Oni March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For heaven's sake STOP! using TurboC. The program should crash if compiled with a decent compiler.

- Shubhro March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The standard doesn't require the program to crash. The behavior is undefined.

- eugene.yarovoi March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what i think... U cant assign the val to pointer... as 'd' gives the ascii for d and assigning it,dosn't justify

- aantrix March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

your answer is correct..

- Anonymous May 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Segmentation fault
*c = 'd';

- abc July 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

segmentation fault

- pramod August 22, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More