Interview Question for Software Engineer / Developers


Team: Development
Country: India




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

Reason for the same is:

When compiler sees such literals "abc" (or "hello world") it stores them in program space(every running code is divided into program space "where machine code exists and can never be changed during the course of program in execution" and data which has stack, heap etc) and when you do p[0] = 'a' this means you are trying to change something in program space which is restricted....

In case you do something like chap p[] = "abc" then "abc" is copied to data space (either stack if its done inside function call or global data segment if its done globally) hence we can do p[0] = 'b' (because now you are modifying data space..

For the reason given by ashot madatyan: when you try to do p[0] = 'a' its a runtime error(or rather seg fault) and during runtime there is nothing like constant/variable...hence we cant say this is the reason for the same.

Thanks
Ashutosh

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

I am able to get the p[0]='a'; it is possible!!

- Adharsh September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This one should give you at least a warning - " char *p = "abc"; " because you are trying to assign a pointer to a non-constant data to a literal constant.
And the "reason behind it" is that the data is constant and cannot be changed.

- ashot madatyan May 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"abc" is a constant string and we can not change the value of the constant string by means of character pointer p.

- sourav June 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

then what is reason behind char a[3]="abc";
a[0]='z';printf("%s",a);output : zbc.According to you(Ashutosh) what you give , compiler can't change the a[0]='z';but we are getting the output--zbc

- neelabh May 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

char *p = "abc" <--- This is immutable. It's a constant. You cannot change it. If you try in a simple program you will get segfault; if you try to p[0] = 'k';

char p[3] = "abc" --> You declared an array and initialized it. You are allowed to changed array elements.

Following from A book on C:

You need to understand that in C array, pointers and Strings are closely related.

Array name can be used as a pointer. (It's a constant pointer you cannot initialized it)

p[i] and * (p + i) are equivalent

s[i] and *(s + i) are equivalent

++p is valid for increment
++s is wrong --> cannot change s

- zero_cool May 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So what is the difference between
char *p = "abc"
and
const char *p = "abc"

- DashDash May 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

with const char *p you cannot change values.

For example
char *p = "abc";
char k[2] = {'l','m'};
p=k;
p[1]='o'; // this works

but


const char *p = "abc";
char k[2] = {'l','m'};
p=k;
p[1]='o'; // this will not work as it's const char pointer

- zero_cool May 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

then what is reason behind char a[3]="abc";
a[0]='z';printf("%s",a);output : zbc.According to you(Ashutosh) what you given reason , compiler can't change the a[0]='z';but we are getting the output--zbc

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

Because p is a pointer that points to an array. p itself is not an array so you cann't subscribe it.

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

char *p = "abc" <--- This is immutable. It's a constant. You cannot change it. If you try in a simple program you will get segfault; if you try to p[0] = 'k';

- zero_cool May 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried to change p[0]='k' and code was successful compile and got out 'kbc'.

After reading the whole discussion, I got understanding,we can not change the value but I am able to compile and getting result also.

- Abhishek July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you copy paste full code here.. also what compiler and os are you using?

- zero_cool July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ashutosh is right here.
Thanks Ashutosh

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

The reason is that p became a constant, but the fundamental reason is that the memory allocated by it is stored in the binary in a section that is not mapped writeable (on Linux it's general .rodata, aka read-only data) hence anything that tries to write in this section will cause a page fault!

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

abc is a literal and this goes into the read only segment..and when u do a char* p ="abc" then this p points to abc..therefore u cannot change it..On the other hand if u do char p[]="abc" then the literal abc goes to the read only section and a copy of that is assigned to char p[] and henc p[0]= z is allowed in this case

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

The memory for string is allocated in the read only area of the data region.While memory for the pointer p is allocated in the stack. Hence compiler doesn't permit us to the the string to which the pointer is pointing. To understand & see how it is happening, see the below program & try to analyze how the memory is allocated.

int main()
{
        int i;
        char *p="Hello";
        int j;
        printf("%p %p %p %p",&i,&p,&j,p);
        return 0;
}

ideone.com/qNREi

- Aashish July 05, 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