Thursday, November 3, 2011

Passing Struct to Function

#include <iostream>
#include <string>
using namespace std;

struct Student
{
  string name;
  int id;
  float gpa;
}; // Student

Student getGeorge()
{
  Student george;
  george.name = "George";
  george.id = 123456;
  george.gpa = 3.4;
  return george;
} // getGeorge

void printRecord(Student&amp; x)
{
  cout &lt;&lt; "Name=" &lt;&lt; x.name;
  cout &lt;&lt; ", ID=" &lt;&lt; x.id;
  cout &lt;&lt; ", GPA=" &lt;&lt; x.gpa;
} // printRecord

int main()
{
  Student s = getGeorge();
  printRecord(s);
  return 0;
} // main

No comments:

Post a Comment