Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to pass nested struct to function by reference in C++

1 Answer

0 votes
#include <iostream>

struct info {
    int data;
};

struct worker {
    info age;
    float salary;
};

void print_struct(const worker& w);

int main() {
    worker w;
    
    std::cout << "Enter Age: ";
    std::cin >> w.age.data;
    std::cout << "Enter Salary: ";
    std::cin >> w.salary;
    
    print_struct(w);
}

void print_struct(const worker& w) {
    std::cout << "age = " << w.age.data << "\n";
    std::cout << "salary = " << w.salary << "\n";
}

   
   
/*
run:
      
Enter Age: 45
Enter Salary: 51000
age = 45
salary = 51000
               
*/

 



answered Dec 5, 2024 by avibootz

Related questions

1 answer 156 views
1 answer 178 views
3 answers 1,568 views
1 answer 180 views
180 views asked Oct 8, 2014 by avibootz
1 answer 44 views
...