C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 10)
10.
What will be the output of the program ?
#include<stdio.h>
int main()
{
void *vp;
char ch=74, *cp="JACK";
int j=65;
vp=&ch;
printf("%c", *(char*)vp);
vp=&j;
printf("%c", *(int*)vp);
vp=cp;
printf("%s", (char*)vp+2);
return 0;
}
Discussion:
84 comments Page 1 of 9.
Nilesh said:
1 decade ago
Pointer always store integer value so cp will store the memory address of location where string "jack " is stored.
Step 1 : vp = &ch;
/*Will store address of ch in vp so while we print content in printf it will print asccii value of 74 i.e "J"*/
Step 2 : vp = &j;
/* It will assign address of j to vp again it will print ascii value of 65 as "A"*/
Step 3 : vp = cp;
/* In this step cp is pointing to memory locatioon where string jack is stored and we r incrementing it by two so it will point to "C" from sring "JACK" and since we hava given %S in printf so it will print content from c onwords ie "CK"*/
So final combined output will be JACK.
Step 1 : vp = &ch;
/*Will store address of ch in vp so while we print content in printf it will print asccii value of 74 i.e "J"*/
Step 2 : vp = &j;
/* It will assign address of j to vp again it will print ascii value of 65 as "A"*/
Step 3 : vp = cp;
/* In this step cp is pointing to memory locatioon where string jack is stored and we r incrementing it by two so it will point to "C" from sring "JACK" and since we hava given %S in printf so it will print content from c onwords ie "CK"*/
So final combined output will be JACK.
(47)
Pradeepa said:
8 years ago
Till now, I didn't clear please explain in short and clear.
(2)
Navya said:
8 years ago
Thanks.
(1)
Imran ahmad said:
8 years ago
Thanks @Nilesh.
(1)
Agii said:
8 years ago
Thanks @Nilesh.
(1)
Sumit said:
9 years ago
Super job, Thank you @Nilesh.
Krishnaveni said:
8 years ago
Thanks for your explanation @Nilesh.
Prashanth said:
10 years ago
Hi,
Can anybody say, how you are telling ASCII values like 74=j and 65=a old tel me.
Can anybody say, how you are telling ASCII values like 74=j and 65=a old tel me.
Siya said:
9 years ago
Good explanation. Thank you @Nilesh.
Ponraj said:
9 years ago
@Nilesh.
I understand this with the help of your explanation. Thank you.
I understand this with the help of your explanation. Thank you.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers