Arjun wants to develop a program using a void pointer 'ptr' to store the address of an integer variable and cast 'ptr' to an integer pointer, and then dereference it to print the value stored at that memory location.Help Arjun to complete the task.Input format :The input consists of an integer N.Output format :The output prints the integer value after dereferencing it.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N ≤ 100Sample test cases :Input 1 :29Output 1 :29Input 2 :76Output 2 :76Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:voidSet 2:*
Question
Arjun wants to develop a program using a void pointer 'ptr' to store the address of an integer variable and cast 'ptr' to an integer pointer, and then dereference it to print the value stored at that memory location.Help Arjun to complete the task.Input format :The input consists of an integer N.Output format :The output prints the integer value after dereferencing it.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N ≤ 100Sample test cases :Input 1 :29Output 1 :29Input 2 :76Output 2 :76Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:voidSet 2:*
Solution
Here is a simple C program that accomplishes the task:
#include <stdio.h>
int main() {
int N;
scanf("%d", &N); // read the input
int *intPtr; // declare an integer pointer
void *ptr; // declare a void pointer
ptr = &N; // store the address of N in the void pointer
intPtr = (int *)ptr; // cast the void pointer to an integer pointer
printf("%d", *intPtr); // dereference the integer pointer and print the value
return 0;
}
This program first reads an integer N from the input. Then it declares a void pointer ptr and an integer pointer intPtr. It stores the address of N in ptr, then casts ptr to an integer pointer and assigns the result to intPtr. Finally, it dereferences intPtr and prints the value.
Similar Questions
Arun is developing a program that demonstrates the use of a constant pointer to a constant integer (const int *). He should take an integer input from the user, use a constant pointer to store its address, and then print both the original value of the integer and the value accessed through the constant pointer.Guide Arun to accomplish the task.Input format :The input consists of an integer.Output format :The first line of output prints "Original value of number: ", followed by the input integer.The second line prints "Value accessed through const pointer: ", followed by the value accessed through the pointer.Refer to the sample output for formatting specifications.Sample test cases :Input 1 :10Output 1 :Original value of number: 10Value accessed through const pointer: 10Input 2 :89Output 2 :Original value of number: 89Value accessed through const pointer: 89Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:constSet 2:*Fill your code hereC (17)12// You are using GCCProvide Custom InputClear
#include <stdio.h>int main(void) { int i = 11; int *ptr1 = &i; int **ptr2 = &ptr1; int ***ptr3 = &ptr2; printf("%p, %p, %p, %p\n", ptr1, ptr2, ptr3, &ptr3); return 0;}If the program output is (where _ is part of the address that is not shown):0x_0b4, 0x_0b8, 0x_0c0, 0x_0c8At address 0x_0b4 is stored 11 .At address 0x_0c8 is stored 200 .
What is the output of the following code snippet?int x = 5;int y = 10;int *ptr = &x;*ptr = y;printf("%d", x);510015Generates a compilation error
In C, what is the result of declaring a pointer as int *ptr; without initialization?Marks : 1Negative Marks : 0Answer hereThe value of the variableptr points to a specific known memory locationptr contains a garbage value as its addressptr points to the memory location 0
PointersWhat will be the output of the following C code?main() { static int a[ ] = {0, 1, 2, 3, 4}; int *p[ ] = {a, a+1, a+2, a+3, a+4}; int **ptr = p; ptr++; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); *ptr++; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); *++ptr; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); ++*ptr; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr);}Options1 1 12 2 23 3 33 4 41 1 12 2 23 3 34 4 41 1 12 2 24 4 43 3 31 1 12 2 23 3 33 3 3
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.