C Quiz
Posted: August 26th, 2004, 11:15 pm
Okay, let's see who knows their C...
The following simplified program compiles and links perfectly.
It produces the following output...
The pointer is NULL in main().
Why?
The following simplified program compiles and links perfectly.
Code: Select all
int alloc_worked_main = 0;
int alloc_worked_sub = 0;
void main()
{
char *ptr = NULL;
set_mem ( ptr );
if (ptr) alloc_worked_main = 1;
printf ("main=%s,\nsub=%s\n", alloc_worked_main, alloc_worked_sub);
}
char * set_mem ( char *ptr )
{
ptr = (char *) malloc ( 256 );
if (ptr) alloc_worked = 1;
return ptr;
}
It produces the following output...
main=0
sub=1
The pointer is NULL in main().
Why?