-12
#include <iostream>

func(void *ptr)
{
 *ptr = NULL;          
}

int main()
{
 void *ptr = (int*)malloc(sizeof(int));   
 func(ptr);
 return 0;   
}

Can Some one Help me in Resolving this. I want to assign NULL to this Pointer. Do Not make any changes in Main().

Prag Rao
  • 1,411
  • 3
  • 11
  • 10

3 Answers3

3

Pointers are passed by value. So change func(void *ptr) to func(void **ptr) and pass in func(&ptr)

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
0

use ptr = NULL; and don't cast the return of malloc.

0

Just Dont cast because C malloc returns void pointer by default. Then the program should be good to run

Krishna Oza
  • 1,390
  • 2
  • 25
  • 50
  • Noo.. Still the same thing !!! I am not able to understand.. cant i do the *ptr = NULL. no one has given exxact reason.. instead of ignoring – Prag Rao Nov 21 '13 at 11:10