main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}
what is the output?



Answers were Sorted based on User's Feedback



main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); ..

Answer / sanath

ANS: NULL
It is a bit tricky question. If u observe carefully then we
are incrementing the pointers p1,p2. When it reached the end
of the string, *p2 points to NULL. We have lost the address
of the starting position.

Is This Answer Correct ?    40 Yes 5 No

main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); ..

Answer / vadivel t

Hi all,

#1 Mannucse's ans is wrong. cos as mentioned "Name" will
not be the output.

#2 Sanath's ans is wrong. Cos at the end of the while loop,
p2 will not point to NULL. It will point to the next byte
to the NULL termination ie., 6th byte.

#3 Shruti's ans is wrong. cos i think she got confused
between assignment(=) and comparisonal(==) operators. And
the statement given as "we cannot copy the value of p1 in
p2, the way its mentioned here" is absolutely wrong.


So,
Lets Analyse the program and how to get the required output.

hav a look on th program again.

main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++ = *p1++)
{
printf("TEST \n");
}
printf("%s\n",p2);
getch();
}

Here, in every iteration of while loop, we are assigning
*p1 to *p2, and incrementing both pointers p1 and p2, After
completion(when *p1 value would be '\0')of the while loop,
first 5 bytes of p2 holds the
characters 'N','a','m','e' '\0'. At the end of while loop
p2 points to the 6th byte in the memory.

So, now printf("%s\n",p2); shall start print the values
from the 6th byte to 20th bytes of the memory which was
allocated dynamically.
----------------------------------
To get the desired output change the printf statement to
printf("%s\n",p2-5);

Now (p2-5) points to the starting address of p2 and will
print the values in the memory till it encounters '\0'
termination. ie., The output would be -> Name

Is This Answer Correct ?    9 Yes 6 No

main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); ..

Answer / vadivel t

In addtion to the answer #5.

printf("%s\n",p2); will print the values from 6th byte to
20th byte.

6th byte to 20th bytes of the memory will contain some
Garbage value. So the output will be a string of garbage
values.


For desired o/p see the ans #5

Is This Answer Correct ?    6 Yes 3 No

main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); ..

Answer / ali fakoor

A part of uninitialized (and/or unowned) memory after (and
including) the sixth byte of the malloc-ed memory will be
printed out until reaching a NULL character in the memory
somewhere!

Is This Answer Correct ?    6 Yes 4 No

main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); ..

Answer / vint

int main()
{
char *p1="Name";
char *p2,*s1,*s2;;
p2=(char *)malloc(20);
s1 = p1;
s2 = p2;
while(*p2++ = *p1++);
printf("%s %s",s1,s2);
return 0;
}

Store the Start address of p1 and p2 before incrementing the pointer so that it could be later used to print the String.

Is This Answer Correct ?    1 Yes 0 No

main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); ..

Answer / mannucse

name

Is This Answer Correct ?    8 Yes 14 No

main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); ..

Answer / shruti

-> while(*p2++ "=" *p1++)
the syntax of while is
while("condition");

in condition statement the assignment operator is used in a
wrong way..
when we are using loop it should be "=="..

we cannot copy the value of p1 in p2, the way its mentioned
here..


** It will either give an error or display some garbage
value in p2 , or no value..
depends on what p2 is initialised to implicitly..

Is This Answer Correct ?    1 Yes 12 No

Post New Answer

More C Interview Questions

Why is it important to memset a variable, immediately after allocating memory to it ?

0 Answers  


With the help of using classes, write a program to add two numbers.

0 Answers   TCS,


code snippet for creating a pyramids triangle ex 1 2 2 3 3 3

4 Answers  


read the folllowing code # define MAX 100 # define MIN 100 .... .... if(x>MAX) x=1; else if(x<MIN) x=-1; x=50; if the initial value of x=200,what is the vlaue after executing this code? a.200 b.1 c.-1 d.50

4 Answers   TCS,


What is action and transformation in spark?

0 Answers  






Give me basis knowledge of c , c++...

5 Answers  


What is the difference between i++ and i+1 ?(in terms of memory)

3 Answers   HCL,


is compiler do read the data line by line or not. ??

6 Answers   LG Soft, Satyam, Tech Mahindra,


Write a simple code fragment that will check if a number is positive or negative.

0 Answers  


what is the most appropriate way to write a multi-statement macro?

1 Answers  


wat is the difference between a definition and declaration? float y;---it looks like a declaration..but it s a definition.how?someone explain

3 Answers   TCS,


What is a newline escape sequence?

0 Answers  


Categories