Structures in C++

0 Rehman . · July 5, 2015
What do we mean when we say " Function can have a return type of structure" ?
example: 

struct database {
int id_number;
int age;
float salary;
};

/* We made a structure named 'database' */

database function();

Post a Reply

Replies

Oldest  Newest  Rating
+1 c student · July 5, 2015
#include <cassert>

// your struct here
struct human {
   char gender;
};

// struct initializer with return type of a struct human
struct human newHuman (void) {
   struct human h;

   h.gender = 'M'; // set member of struct

   return h; // return struct as if it were a type
}

int main () {
   struct human Bob = newHuman(); // returned into a struct human type

   assert (Bob.gender == 'M'); // check if it worked
   
   return 0;
}
0 Rahul Ragesh · July 7, 2015
In C++, I have come across situations where I needed a function to return a string, but it isn't directly possible to do that. So I made a structure with only one data member of String type...then I Made a structure instance and returned the structure...thereby returning a string the structure held. May not be related to what you asked, but its something you can think about!

struct demo
{
 char name[20];
};

demo item;

demo getname()
{
  item.name = " Mobile"; 
 return item;
}

void main()
{
 cout<<" Name : "<<getname().name;
}
 
  • 1

C++

103,663 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator