0

I am a beginner at programming. What are the differences between the use of malloc() mentioned below? I use the first method, but I have seen the second one being used too. I'm a little confused. Thanks for your help :)

int *ptr;
ptr = malloc(sizeof(int));
int *ptr;
ptr = (int *)malloc(sizeof(int));
Diwas_lufy
  • 45
  • 6
  • The version with the cast works when C++ compilers look at the code; the first does not. However, C and C++ are different languages, so it usually isn't important that the C code works with a C++ compiler too. – Jonathan Leffler Feb 03 '22 at 06:11

0 Answers0