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

Define C in your own Language.

0 Answers   Motorola,


What is the equivalent code of the following statement in WHILE LOOP format?

0 Answers  


Why header file is used in c?

0 Answers  


What is the difference between array and structure in c?

0 Answers  


What is the difference between c &c++?

0 Answers  






how to convert an char array to decimal array

4 Answers  


What is pass by reference in functions?

0 Answers  


#include main() { char s[] = "Bouquets and Brickbats"; printf(" %c, ",*(&s[2])); printf("%s, ",s+5); printf(" %s",s); printf(" %c",*(s+2)); }

0 Answers   Wilco,


write a program to reverse a every alternetive words in a string in a place. EX: Input is "this is the line of text" Output should be "shit is eht line fo text" Please any one tell me code for that.

0 Answers   TCS,


What's the best way to declare and define global variables?

7 Answers  


What is the size of empty structure in c?

0 Answers  


what is compiler

6 Answers  


Categories