-5

What does char*ptr=(char*)&i;exactly do in the following code

#include <stdio.h>
 int main()
 {
  int i=32;
  char*ptr=(char*)&i;
  printf("%d",*ptr);
  return 0;
 }
P.Bendre
  • 55
  • 1
  • 6

1 Answers1

4
char* ptr=(char*)&i;

i is of type int. So you are trying to cast address of i as a character pointer and assign it to a local variable called ptr. This way each byte stored in i can be read. Read more on pointers to understand in detail.

J...S
  • 5,079
  • 1
  • 20
  • 35
Naveen KH
  • 153
  • 2
  • 11