Node:Another variable parameter example, Previous:Passing pointers correctly, Up:Variable parameters



Another variable parameter example

There is nothing mysterious about pointers, but they can be tricky. Here is another example.

Notice that the pointers in both this example and the example above are dereferenced with asterisks before they are used (for instance, when the contents of the location pointed to by height_ptr are multiplied by the integer hscale with the line *height_ptr = *height_ptr * hscale; in the function scale_dimensions below).

#include <stdio.h>

int main();
void scale_dimensions (int *, int *);

/* Scale some measurements */

int main()
{
  int height,width;

  height = 4;
  width = 5;

  scale_dimensions (&height, &width);

  printf ("Scaled height = %d\n", height);
  printf ("Scaled width = %d\n", width);

  return 0;
}


void scale_dimensions (int *height_ptr, int *width_ptr)
{
  int hscale = 3;        /* scale factors */
  int wscale = 5;

  *height_ptr = *height_ptr * hscale;
  *width_ptr = *width_ptr * wscale;
}