511 views
0 votes
0 votes
What changes must be done for printing value 5.

#include <stdio.h>

int main()
{
 int var;  /*Suppose address of var is 2000 */

 void *ptr = &var;
 *ptr = 5;
 printf("var=%d and *ptr=%d",var,*ptr);
             
 return 0;
}

2 Answers

1 votes
1 votes

changes to be made are in bold

#include <stdio.h>

int main()
{
 int var;  /*Suppose address of var is 2000 */

 void *ptr =&var;
 var= 5;
 printf("var=%d and *ptr=%d",var,*(int*)ptr);
             
 return 0;
}

0 votes
0 votes
Before accesing the void pointer it should be typecast to respective datatype

#include <stdio.h>
int main()
{
int var;  /*Suppose address of var is 2000 */

 void *ptr = &var;
*(int *)ptr = 5;
printf("var=%d and *ptr=%d",var,*(int *)ptr);

 return 0;
}

Related questions

0 votes
0 votes
1 answer
1
anonymous asked May 14, 2018
580 views
Please explain solution in brief . #include <stdio.h void f(char ); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char ...
0 votes
0 votes
1 answer
2
Debargha Mitra Roy asked 2 days ago
39 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
1 votes
1 votes
2 answers
3
0 votes
0 votes
1 answer
4
ermp888 asked Jun 30, 2018
669 views
6. Would the following program compile?main( ) { int a = 10, *j; void *k; j = k = &a; j++ ; k++; printf ("\n %u %u", j, k ) ; }Please explain above program with some exam...