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,855 questions

51,776 answers

573 users

How to return struct from a function in C++

3 Answers

0 votes
#include <iostream>

struct ST {
    int first, second, third;
} typedef ST;
 
ST get_struct_value() {
    ST st;
     
    st.first = 123;
    st.second = 87;
    st.third = 3;
     
    return st;
}
 
int main() {
    auto st = get_struct_value();
    
    std::cout << st.first << " " << st.second << " " << st.third;
}
 
 
 
 
/*
run:
 
123 87 3
 
*/

 



answered May 14, 2021 by avibootz
edited Dec 8, 2023 by avibootz
0 votes
#include <iostream>

struct ST {
	int x, y;
};

ST get_struct_value(const int n) {
	return {n, n * 3};
}


int main(void)
{
   	auto [x, y] = get_struct_value(5);

    std::cout << x << ' ' << y;
}
 
 
 
 
/*
run:
 
5 15
 
*/

 



answered Dec 8, 2023 by avibootz
0 votes
#include <iostream>

struct ST {
    int first, second, third;
} typedef ST;
 
ST get_struct_value() {
    ST st;
     
    st.first = 123;
    st.second = 87;
    st.third = 3;
     
    return st;
}
 
int main() {
    auto [x, y, z] = get_struct_value();
 
    std::cout << x << ' ' << y << ' ' << z;
}
 
 
 
 
/*
run:
 
123 87 3
 
*/

 



answered Dec 8, 2023 by avibootz

Related questions

1 answer 150 views
1 answer 191 views
1 answer 306 views
1 answer 245 views
1 answer 154 views
154 views asked Oct 25, 2022 by avibootz
1 answer 91 views
1 answer 110 views
...