What does the line "void *ptr = arr;" do?123456789101112#include <stdio.h>int main() { int arr[2] = {10, 20},i=0; void *ptr = arr; while(i<2) { printf("%d ", *((int *)ptr + i)); i++; } return 0;}Marks : 1Negative Marks : 0Answer hereModifies the values of the array arr.Initializes a void pointer to the array arr.Prints the memory addresses of the array arr.Results in a compilation error.ClearPrev
Question
What does the line "void *ptr = arr;" do?123456789101112#include <stdio.h>int main() { int arr[2] = {10, 20},i=0; void *ptr = arr; while(i<2) { printf("%d ", *((int *)ptr + i)); i++; } return 0;}Marks : 1Negative Marks : 0Answer hereModifies the values of the array arr.Initializes a void pointer to the array arr.Prints the memory addresses of the array arr.Results in a compilation error.ClearPrev
Solution
The line "void *ptr = arr;" initializes a void pointer to the array arr. This means that the pointer ptr is pointing to the memory location of the array arr. Void pointers are a type of pointer that can point to any data type. In this case, it is used to point to an integer array.
Similar Questions
What does the line "void *ptr = arr;" do?123456789101112#include <stdio.h>int main() { int arr[2] = {10, 20},i=0; void *ptr = arr; while(i<2) { printf("%d ", *((int *)ptr + i)); i++; } return 0;}
What is the output of the following code?int arr[5] = {10, 20, 30, 40, 50};int *ptr = arr;cout << *(ptr + 2);10203040
What is the result of the following code?#include <stdio.h>int main() { int arr[5] = {1, 2, 3, 4, 5}; int *ptr = (int *)(&arr + 1); printf("%d\n", *(ptr - 1)); return 0;}
What is the output of the following code?int arr[5] = {1, 2, 3, 4, 5};int *p = arr;printf("%d\n", p[2]);
What is the output of the following code?1234567891011#include <stdio.h>int main() { char c = 'K'; char* ptr = &c; (*ptr)++; printf("%c ", *ptr); (*ptr)--; printf("%c", *ptr); return 0;}
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.