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

How would you use the functions fseek(), freed(), fwrite() and ftell()?

0 Answers   Aspire, Infogain, TISL,


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

3 Answers   HCL,


code for reverse alternate words from astring

1 Answers   IBM,


What are preprocessor directives in c?

0 Answers  


Write a program to compare two strings without using the strcmp() function

42 Answers   Accenture, Arba Minch University,






please send me the code for multiplying sparse matrix using c

0 Answers  


differnce between do and do while

3 Answers   DOEACC,


write a pgm to print 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1

3 Answers  


how to find the size of the data type like int,float without using the sizeof operator?

13 Answers  


Explain the difference between malloc() and calloc() in c?

0 Answers  


what is the difference between call by value and call by reference?

5 Answers   Genpact, Global Logic, Infosys,


HOW CAN ADD OUR FUNCTION IN LIBRARY.

5 Answers  


Categories