GeeksforGeeks » Interview Questions
Adobe Interview Question for Software Engineer/Developer (Fresher) about Aptitiude, Arrays, C++, Dat
(3 posts)-
Predic the output of the following code snippet.
main()
{
char *ch1 = "Rajesh";
char *ch2;
ch2 = (char *)malloc (20);
memset (ch2.0.20);
while (*ch2++ = *ch1++)
printf("%s\n",ch2);
}The answer is empty output or no output. I executed this on Linux terminal but it shows a few blank lines followed by the Prompt.
Can anyone please explain what this function is trying to do and why do we get a few blank lines as the output?
-
Sorry for the typo. The above function should have been typed as memset (ch2,0,20);
-
The function is copying character by character of string "Rajesh" to memory pointed to by ch2 in a while loop. In the same while expression the pointers are incremented so in the printf stmt ch2 is always pointing to a null string.
if u change the printf to the below stmnt
printf("%s\n",ch2-1);u can see what ch2 is pointing to before incrementing it.
Reply
You must log in to post.