Answer P16

When you compile and run the program, you will get a run-time error.


error display
#include <stdio.h>

void newFunction( int *p )  
{
  printf("  p=%d\n", *p ); 
}

void main ( void )
{
  int a = 77 ;
  printf("a=%d\n", a ) ;
  newFunction( a ) ;
}

This is a case where the caller main() and the callee newFunction() are not properly coordinated.

main() passes the contents of the variable a (which is 77):

newFunction( a )

But newFunction(int *p) expects the address of an int variable (a pointer to an int). So when it calls

printf("  p=%d\n", *p )

it tries to go to address 77 in memory. But 77 points to a place in memory that this program does not have access to. The operating system does not let the program do this, and kills it.



Back to Puzzle Home