0

I'm learning C and I've found something like that.

#include <stdio.h>

void func(int* y) {
  int x = 1;
  *y = 2;
  y = &x;
}

int main(int argc, char** argv) {
  int x = 3;
  int* x_ptr = &x;
  printf("Value before call: %d\n", x);
  printf("Pointer before function call: %p\n",(void*)x_ptr);
  func(x_ptr);
  printf("Value after call: %d\n", x);
  printf("Pointer after function call: %p\n",(void*)x_ptr);
  return 0;
}

I don't know why (void*)x_ptr int* casting is used. Why use int* to void* casting? Thank you for any help.

MSalters
  • 173,980
  • 10
  • 155
  • 350

1 Answers1

1

I copy from another question:

Casting to void * for arguments to the %p format in a variadic function is actually required by the language standard. – Jens Apr 17 '11 at 10:54

Printing pointers in C

LPs
  • 16,045
  • 8
  • 30
  • 61
G. C.
  • 387
  • 2
  • 6
  • For a good answer, you should at least provide a reference to the corresponding section(s) in the standard. – Ctx Dec 13 '19 at 11:21