Amazon Interview Question for Software Engineer / Developers






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

10. differences between
char *str = "SIVA" and char str1[]="SIVA"

str is a pointer to a constant string. str1 is a constant pointer.

sub questions:
1) where string literal i.e "SIVA" will be stored ( heap , stack, data or code segments) in above both cases.
str - code segment
str1 - stack

2) char *str = "SIVA";
str[0] = 'f' ;
any error ? compile time error or run time error ?

run time error, as you trying to modify a constant string.

3) char *p = (char *) str;
p[0] = 'f' ;
any error ? compile time error or run time error ?

2) reasoning still applies here. runtime error.

- Messi February 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

>>
1) where string literal i.e "SIVA" will be stored ( heap , stack, data or code segments) in above both cases.
str - code segment
str1 - stack
>>

str points to Read only data sigment

- siva.sai.2020 February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@SB : isnt str1 a pointer constant (rather than const ptr). Sections 2 and 3 are perfect!

- AB February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

str - code segment might save on const page with other constant var , might be on the code segment.

- Charles December 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. code segment (read only)
2 & 3: Compile time error.

- Tulley February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

>>
1. code segment (read only)
>>
wrong.

Read only data segment is correct answer .

- siva.sai.2020 February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It depends on the compiler about where it places data.
For ex. 1. On TC, there will be no errors and everything should work fine, so we can say that str is stored in data or stack area (so it can be modified) and str1 is stored in stack area.
2. On GCC, str seems to be stored in read-only section i.e. code area and so it can be modified, whereas str1 is stored on stack. We will encounter run time errors for str as it is stored in code area.

- Nitin February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.data segment.
2.no error.
3.not sure

- PKT February 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Read only data segment.

In both the cases it will give run time error.

- Anonymous March 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. str is a pointer to a constant string, str1 is a constant pointer
don't know where str is stored, but str1 is stored in stack
2. run time error because we try to change a constant
3. run time error because of the same reason in 2.

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

In function ‘int main()’:
warning: deprecated conversion from string constant to ‘char*’

So in this case char *str = "SIVA"

if we try to modify it we will get segmentation fault

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main(){
	char *p = "We";
	char *w = p;
	w[0] = 'H';
	printf("%s", w);

	return 0;
}

2,3. Run-Time error in minGW, since we are trying to modify:
char * str = "blah"; //which is equivalent to
const char * str = "blah";

However, note that:
const char * str = "blah";//is not equivalent to
const (char *) str = "blah";

Hope it helps.

- TheGhost February 20, 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