Quote:
Originally Posted by SATYAN JANA
A good question usually asked at interviews
I will add one more here
Code:
void AllocateMem(int *p)
{
p = (int*)malloc(sizeof(int));
*p = 6;
return;
}
void main()
{
int *a;
AllocateMem(&a);
printf("[%d]",*a);
}
Gives an exception. I want the function to allocate the memory and want that to use in main. How?
Scroll down for an answer
Code:
void AllocateMemory(int **p)
{
*p = (int*)malloc(sizeof(int));
**p = 6;
return;
}
void main()
{
int *a;
AllocateMemory(&a);
printf("[%d]",*a);
}