Puzzles F11 ... F20

Pointers and Parameters

Parameter passing in C is always done by call by value. This means that when a function is called, arguments of the function call are evaluated and the values are copied into the parameters of the function. As the function executes it works with the values in its parameters. Changing these values does not affect any other variable in the program.


Puzzle F11 — Review of Call by Value

What does the following code write to the monitor?

#include <stdio.h>

void myFunction( int x )  
{
  printf("  x=%d\n", x );
  x = 123;
  printf("  x=%d\n", x );
}

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

The diagram shows main() and myFunction(), each as a module with its own local variable or parameter. The variable a can be seen only by main() and the parameter x can be seen only by myFunction(). When myFunction() is called by main() the value in a is copied into x. In this example 77 is copied into x.

Changes that myFunction() make to its parameter x do not affect a. In this example, x starts out as 77, but then is changed to 123. But this change does not affect a.


Puzzle F12 — Review of Call by Value

What does the following code write to the monitor?

#include <stdio.h>

void myFunction( int x )  
{
  printf("  x=%d\n", x );
  x = 123;
  printf("  x=%d\n", x );
}

void main ( void )
{
  int a = 77;
  printf("a=%d\n", a );
  myFunction( a+11 );
  printf("a=%d\n", a );
  
  system("pause");
}

In this puzzle, the argument in the function call is the expression a+11. When the function is called, the expression is evaluated, and the resulting value of 88 is copied into the parameter x.


Puzzle F13 — Review of Call by Value

What does the following code write to the monitor?

#include <stdio.h>

void functionB( int y )  
{
  printf("    y=%d\n", y );
  y = 999;
  printf("    y=%d\n", y );
}

void functionA( int x )  
{
  printf("  x=%d\n", x );
  x = 123;
  functionB( x );
  printf("  x=%d\n", x );
}

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

In this puzzle main() calls functionA() which calls functionB(). Of course, call by value is used for all these calls and changes made by a function to its parameter do not affect any variable in the caller.


Puzzle F14 — Parameter is a Pointer

What does the following code write to the monitor?

#include <stdio.h>

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

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

Call by value is used in this example, also. However, the parameter p of newFunction() requires a value that is of type pointer to int.

main() supplies such a value in the function call newFunction( &a ). The expression &a evaluates to the address of a. (Often this is called a pointer to a.)

When newFunction() is called, the address of a is copied into the parameter p. Now newFunction() can access a by following the pointer. The statement

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

follows the pointer in p to get a value (which is then printed). The statement

*p = 123;

follows the pointer in p to the location in which to store the value of the expression on the right of the =. This location is the variable a, which is then changed.


Puzzle F15 — Contents of a Pointer

Does the following code compile? If so, what does it write to the monitor?

#include <stdio.h>

void newFunction( int *p )  
{
  printf("  p=%d\n", p ); /* Note: no * before p */
}

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

It looks like printf(" p=%d\n", p ) gets the contents of p and prints it. But what is that?


Puzzle F16 — Mistake

Let us make a dreadful mistake. This time, intentionally. (Unlike those many unintentional dreadful mistakes.) What is the dreadful mistake? Will the program compile? What will happen when it runs?

#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 ) ;
  
  system("pause") ;
}

It looks like printf(" p=%d\n", *p ) follows the pointer in p to find a value to print . But what is that?


Puzzle F17 — Passsing a Pointer value through two Functions

Of course, functions can call functions, and pass pointers along. What does the following code write to the monitor?

#include <stdio.h>

void functionB( int *y )  
{
  printf("    *y=%d\n", *y );
  *y = 999;
  printf("    *y=%d\n", *y );
}

void functionA( int *x )  
{
  printf("  *x=%d\n", *x );
  functionB( x );          /* Note this!! */
  printf("  *x=%d\n", *x );
}

void main ( void )
{
  int a = 77;
  printf("a=%d\n", a );
  functionA( &a );
  printf("a=%d\n", a );
  
  system("pause");
}

main() calls functionA with a pointer to a:

functionA( &a )

functionA then follows this pointer (now contained in its parameter x) and prints the contents of what it points to.

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

Then functionA calls functionB with the value in x:

functionB( x )

Bugs and more bugs!  Look carefully. The parameter x contains a pointer to a. This value is what we want to pass on, and the call functionB( x ) does this. The call

functionB( &x )

would pass a pointer to x (not what we want). The call

functionB( *x )

would pass the value in a , 77, which also would be wrong.

The call functionB(x) copies the value in x into the parameter y of the function. The function now has a pointer to a. When functionB executes

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

it follows y and prints out what it points to. Next the function executes

*y = 999

which follows the pointer in y and stores 999 in the variable a.


Puzzle F18

Of course, a function can be called several times, with different arguments. What does the following write to the monitor?

#include <stdio.h>

void aFunction( int *p )  
{
  *p = 99; 
}

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

  aFunction( &a ) ;
  printf("a=%d  b=%d\n", a, b ) ;

  aFunction( &b ) ;
  printf("a=%d  b=%d\n", a, b ) ;
  
  system("pause") ;
}

In the first call, the parameter of aFunction() points to a. In the second call, it points to b.


Puzzle F19 — The classic swap mistake

In the following, main() calls swap() to reverse the values in the variables a and b. But what really happens?

#include <stdio.h>

void swap( int x, int y )  
{
  int temp;

  printf("  x=%d  y=%d\n", x, y ) ;
  temp = x;
  x = y;
  y = temp;
  printf("  x=%d  y=%d\n", x, y ) ;
   
}

void main ( void )
{
  int a = 44, b = 77 ;

  printf("a=%d  b=%d\n", a, b ) ;
  swap( a, b ) ;
  printf("a=%d  b=%d\n", a, b ) ;
  
  system("pause") ;
}

This puzzle shows a classic C programming error. Be sure you understand what goes wrong.


Puzzle F20

The swap() function in the previous puzzle did not work. Look at the following code (repeated from puzzle 19). Your job is to fix it so that swap() works correctly.

#include <stdio.h>

void swap( int x, int y )  
{
  int temp;

  printf("  x=%d  y=%d\n", x, y ) ;
  temp = x;
  x = y;
  y = temp;
  printf("  x=%d  y=%d\n", x, y ) ;
   
}

void main ( void )
{
  int a = 44, b = 77 ;

  printf("a=%d  b=%d\n", a, b ) ;
  swap( a, b ) ;
  printf("a=%d  b=%d\n", a, b ) ;
  
  system("pause") ;
}

Hint: you can do this by inserting sixteen characters.


— Return to the main contents page