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

What's the difference between constant char *p and char * constant p?

0 Answers   Celstream,


main() { int i=0; while(+(+i--)!=0) i-=i++; printf(i); }

3 Answers  


7-Given an index k, return the kth row of the Pascal's triangle. For example, when k = 3, the row is [1,3,3,1]. For reference look at the following standard pascal’s triangle.

0 Answers  


What is the meaning of c in c language?

0 Answers  


What is difference between union and structure in c?

0 Answers  






Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..

0 Answers   Huawei,


What is the use of static variable in c?

0 Answers  


Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it?

3 Answers  


What is a protocol in c?

0 Answers  


Which header file should you include if you are to develop a function which can accept variable number of arguments?

0 Answers   TCS, TISL,


Explain how do you convert strings to numbers in c?

0 Answers  


If errno contains a nonzero number, is there an error?

0 Answers  


Categories