Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

passing structure to function in c++ example

#include <iostream>
using namespace std;

struct Person {
    char name[50];
    int age;
    float salary;
};

void displayData(Person);   // Function declaration

int main() {
    Person p;

    cout << "Enter Full name: ";
    cin.get(p.name, 50);
    cout << "Enter age: ";
    cin >> p.age;
    cout << "Enter salary: ";
    cin >> p.salary;

    // Function call with structure variable as an argument
    displayData(p);

    return 0;
}

void displayData(Person p) {
    cout << "
Displaying Information." << endl;
    cout << "Name: " << p.name << endl;
    cout <<"Age: " << p.age << endl;
    cout << "Salary: " << p.salary;
}
Source by www.programiz.com #
 
PREVIOUS NEXT
Tagged: #passing #structure #function
ADD COMMENT
Topic
Name
8+5 =