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
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 |
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 |
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 |
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 |
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 |
-> 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 |
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)
code for reverse alternate words from astring
What are preprocessor directives in c?
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
differnce between do and do while
write a pgm to print 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1
how to find the size of the data type like int,float without using the sizeof operator?
Explain the difference between malloc() and calloc() in c?
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.