Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - Another variable parameter example

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;
}

 
 
  Published under free license. Design by Interspire