void alloco(int *ppa)
{
int i;
printf("inside alloco %d\n",ppa);
ppa = (int *)malloc(20);
ppa[15] = 9;
printf("size of a %d \n", sizeof(ppa));
for(i=0;i<20;i++)
printf("a[%d] = %d \n", i, ppa[i]);
}
int main()
{
int *app = NULL;
int i;
printf("inside main\n");
alloco(app);
for(i=0;i<20;i++)
printf("app[%d] = %d \n", i, app[i]);
return(0);
}
Basically all I wanted to do is to pass a null pointer from my main
to a function(alloco
) which allocates memory/fills the same location the pointer is pointing to and returns. I am getting local prints correctly that is inside function(alloco
) but not in main
.
Am I doing anything wrong here?
C
uses pass by value for function parameter passing. You can change the value at address pointed by app
[i.e., ppa
, in alloco()
] from alloco()
but cannot change the value of app
. In other words, ppa
is local to function alloco()
. Changing the value of ppa
won't be affecting (reflected to) app
present in main()
.
If you want to change the value of app
itself from alloco()
, you need to pass a pointer to app (Pointer-to-Pointer).
Apart from this issue, in your code
ppa = (int *)malloc(20);
is wrong. What you wanted is actually
ppa = malloc(20 * sizeof (int)); //argument is in bytes, not in `int` or any elements
or, better,
ppa = malloc(20 * sizeof * ppa); //data type integrity
Also, it's worthy to mention that please do not cast the return value of malloc()
and family in C
.
One more thing, while printing a pointer, use %p
format specifier, while printing size_t
, use %zu
, like
printf("inside alloco %p\n",ppa);
and
printf("size of a %zu\n", sizeof(ppa)); //sizeof returns size_t
You need this:
void alloco(int **ppa)
{
int i;
printf("inside alloco %p\n",ppa);
*ppa = malloc(20 * sizeof(int));
(*ppa)[15] = 9; // rather pointless, in the loop below (*ppa)[15] will be
// overwritten anyway
printf("size of a %d \n", sizeof(*ppa)); // rather pointless, not sure
// what you want to print here
for(i = 0; i < 20; i++)
printf("a[%d] = %d \n", i, (*ppa)[i]);
}
int main()
{
int *app = NULL; // you can drop the '= NULL', it's useless, because
int i; // alloco(&app) will change the value of app anyway
printf("inside main\n");
alloco(&app);
for(i = 0; i < 20; i++)
printf("app[%d] = %d \n", i, app[i]);
return 0;
}
In your program you pass a pointer to alloco
which will be in the ppa
parameter. This ppa
parameter is just like a local variable inside of alloco
and when you modify it, the original value you passed to the function in main
(app
) won't be modified.
In the corrected version, we pass a pointer to app
. In alloco
we dereference that pointer and write the malloced value to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With