Passing a structure's address and using a pointer with the new notation to reference the structure directly. - C++ Data Type

C++ examples for Data Type:struct

Description

Passing a structure's address and using a pointer with the new notation to reference the structure directly.

Demo Code

#include <iostream>
#include <iomanip>
using namespace std;
struct Employee  // declare a global data type
{
   int id;//w  w  w.  ja v  a 2s .  com
   double payRate;
   double hours;
};
double calcNet(Employee *);  //function prototype
int main()
{
   Employee emp = {6782, 8.93, 40.5};
   double netPay;
   netPay = calcNet(&emp);    // pass an address
   cout << setw(10) << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(2);
   cout << "The net pay for employee " << emp.id << " is $" << netPay << endl;
   return 0;
}
double calcNet(Employee *pt)   // pt is a pointer to a
{                            // structure of Employee type
return(pt->payRate * pt->hours);
}

Result


Related Tutorials