How to pass struct to function as argument in C++

1 Answer

0 votes
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

struct user {
	string name;
	int age;
};

void print(user u) {
	cout << u.name << " " << u.age << endl;
}

void set(user u)  {
	u.age = 98;
}

int main()
{
	user u = { "tom", 54 };
	
	print(u);

	set(u);

	print(u);

	return 0;
}


/*
run:

tom 54
tom 54

*/

 



answered May 24, 2018 by avibootz

Related questions

...