I'm trying to figure out how to allocate a block of memory in a function and pass back a pointer to that block through one of the arguments. This is a C program. I seem to be having some trouble. Here's the code:
void foo(char *ptr)
{
if (!(ptr = malloc(size)))
printf("error");
/* code here */
printf("buffer address: %i\n", (int)buffer);
}
int main()
{
char *ptr;
ptr = NULL;
foo(ptr);
printf("buffer address: %i\n", (int)buffer);
}
And the result is:
buffer address: 142385160
buffer address: 0
but I was expecting something like:
buffer address: 142385160
buffer address: 142385160
What am I doing wrong?