I wrote the following simple C++ structure implementation to see the usage of pointer to structure.
#include <iostream>
struct Employee
{
std::string name;
std::string sex;
int e_id;
}emp[2];
void printEmployee(struct Employee *e)
{
std::cout << e->name << std::endl;
std::cout << e->sex << std::endl;
std::cout << e->e_id << std::endl;
}
int main(int argc, char const *argv[])
{
struct Employee *e_ptr;
e_ptr = emp;
for (int i = 0; i < 2; ++i)
{
std::getline(std::cin, emp[i].name);
std::getline(std::cin, emp[i].sex);
std::cin >> emp[i].e_id;
}
for (int i = 0; i < 2; ++i)
{
printEmployee(e_ptr+i);
std::cout << std::endl;
}
return 0;
}
The inputs are
John Smith
male
123
Sarah Collin
female
After these input the program shows the output although the code is supposed to take one more input. The output is
John Smith
male
123
Sarah Collin
0
But If I don't include int e_id
as a member then the code works perfectly.