Adobe Interview Question for Software Engineer / Developers


Country: India




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

the 2nd declaration (char *str) declares a pointer to a char... this pointer is placed on the current memory stack but the data itself ("/root//foo/bar") is stored elsewhere, not on the current stack and not at that same location. So you cannot (should not) change the value using str[in1] = str[in2]. It will lead to seg fault as you have rightly noticed.

But if you have to change the value, you use double indirection (using ** or &) and make the change.

- JustCoding October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Using ** and & will just give you undefined behavior. The compiler may actually place the second string in read-only memory. If you try to use ** and & you will get a memory error.

- Anonymous October 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello, Where is the ROM concept will come.
ROM is permanent storage device. Always variables are stored in Mainmemory i.e RAM. since it is local variable to it is stored in Stack segment.

- Jaya November 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The pointer, *str, is stored on the stack. What it is pointing to need not be on the stack.

- Anonymous November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

char s[] = "/root//foo/bar" ;
char *str = "/root//foo/bar" ;

-> s is a constant pointer it will point to base address of first element, we can't change address but we can change value.
ex: scanf("%s",s);
strcpy(s,s1);// if s1 has some address of string
s[0]='d',s[1]='j'........
-> str is a pointer to an constant integer, string will store in Read only memory in Data segment that's why we can't change value but we can change address.

str=malloc(size_t size);
scanf("%s",str);

- pmahesh.417 February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Only problem is Char *str is a read only string literal. You cannot assign anything to this.

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

Essentially the second statement is asking compiler to allocate memory for the string literal, which is read only. For a complete explanation please refer to the anwers on stack overflow question -

stackoverflow.com/questions/1577765/why-must-a-pointer-to-a-char-array-need-strcpy-to-assign-characters-to-its-array

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

char *str = "foo"; is an unmodifiable string as described by the C standard. Any attempt to modify it results in undefined behavior. Undefined behavior means the behavior of your program is no longer described by the C standard so your implementation may do whatever. In this case, it gives "segmentation fault." Another valid response would be: "system("rm -fr /");" if you were on a *nix based system.

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

#include<stdio.h>
void main()
{
	char s[]="nimesh";
	char *str="nimesh";
	printf("s=%s\n",s);
	printf("str=%s\n",str);
	s[1]='a';   // this works fine
	str[1]='a';  // this gives segmentation fault as it is constant string and stored in Read only area....
	printf("s=%s\n",s);
	printf("str=%s\n",str);

}

- kathrotiyanimesh May 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

the 2nd one returns a single reference to the starting of the string literal where that assignment cannot be done as it needs 2 pointers on those elements..where as in the 1st one ,we have access to all the elements in the array

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

the 2nd one returns a single reference to the starting of the string literal where that assignment cannot be done as it needs 2 pointers on those elements..where as in the 1st one ,we have access to all the elements in the array

- Karthik vvs October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

but how is it possible for both to have same name?

- anon October 23, 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