-6

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

3
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
  • 4,903
  • 1
  • 17
  • 35
Naveen KH
  • 123
  • 1
  • 11