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;
}
JCK
J65K
JAK
JACK
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
85 comments Page 1 of 9.

Nilesh said:   2 decades 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.
(49)

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:   9 years ago
Thanks @Nilesh.
(1)

Krishnaveni said:   8 years ago
Thanks for your explanation @Nilesh.

Nikhil navgire said:   8 years ago
Thanks @Nilesh well explained!

Shubham said:   9 years ago
@Sriram.

Because there is %c format specifier in the printf function.

Sivaraj MCA said:   9 years ago
The void pointer can't accept any arithmetic operation then how it is possible?

Sriram said:   9 years ago
In the 9th line it is integer type casting how 'A' is displayed.


Post your comments here:

Your comments will be displayed after verification.