
December 30th, 2012, 04:23 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 112
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
|
|
|
Question about passing pointer to function
i'm reading about memory allocation, and i saw an example of a wrong way and a right way to allocate memory from functions.
i have a question about it:
why is this wrong:
first way:
Code:
void func(int *p)
{
p = (int *) malloc(3*sizeof(int));
}
void main()
{
int *pm = NULL;
func(pm);
}
and this is right:
second way:
Code:
void func(int **p)
{
*p = (int *) malloc(3*sizeof(int));
}
void main()
{
int *pm = NULL;
func(&pm);
}
why isn't the allocated memory using the first way accessible?
doesn't p hold pm's address?
thanks in advanced!
|