main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}
what is the output?
Answer Posted / 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 |
Post New Answer View All Answers
What does 2n 4c mean?
Explain what standard functions are available to manipulate strings?
How do you sort filenames in a directory?
a c variable cannot start with a) an alphabet b) a number c) a special symbol d) both b and c above
"C" language developed by "Dennis Ritchie" at AT & T. his remarks are a) too general, too abstract b) could deal with only specific problems c) lost generality of BCPL and B restored d) no remarks
Difference between pass by reference and pass by value?
Write a program to print all permutations of a given string.
write an interactive C program that will encode or decode a line of text.To encode a line of text,proceed as follows. 1.convert each character,including blank spaces,to its ASCII equivalent. 2.Generate a positive random integer.add this integer to the ASCII equivalent of each character.The same random integer will be used for the entire line of text. 3.Suppose that N1 represents the lowest permissible value in the ASCII code,and N2 represents the highest permissible value.If the number obtained in step 2 above(i.e.,the original ASCII equivalent plus the random integer)exceeds N2,then subtract the largest possible multiple of N2 from this number,and add the remainder to N1.Hence the encoded number will always fall between N1 and N2,and will therefore always represent some ASCII character. 4.Dislay the characters that correspond to the encoded ASCII values. The procedure is reversed when decoding a line of text.Be certain,however,that the same random number is used in decodingas was used in encoding.
What is the g value paradox?
Write a program to compute the similarity between two strings - The program should get the two strings as input - Then it will output one single number which is the percentage of similarity between the two strings
Can a program have two main functions?
What is uint8 in c?
What is use of bit field?
any "C" function by default returns an a) int value b) float value c) char value d) a & b
In c programming write a program that will print 10 multiples of 3 except 15,18,21 using looping