I want to print the data using pointers i've done it using for but not getting how to use pointer in array of structures so is there any one who can help me and get me out of it....
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main()
{
struct employee
{
char e_name[20];
int e_id;
int e_age;
char e_degree[20];
};
struct employee e[3];
int i = 1;
cout << "Enter the Employee Data:" << endl;
for (i=1; i<4; i++)
{
cout << "Data of Employee #" << i << ":" << endl;
cout << "Enter Employee Name:" << endl;
cin >> e[i].e_name;
cout << "Enter Employee ID:" << endl;
cin >> e[i].e_id;
cout << "Enter Employee age:" << endl;
cin >> e[i].e_age;
cout << "Enter Employee Highest Degree (Graduation/Masters/Mphil/):" << endl;
cin >> e[i].e_degree;
}
struct employee *ptr;
ptr = &e[3];
for (i=1; i<4; i++)
{
cout << e[i].e_name, e[i].e_id, e[i].e_age, e[i].e_degree;
cout << "Data of Employee #" << i << ":" << endl;
cout << "Name: " << ptr->e_name << endl;
cout << "ID: " << ptr->e_id << endl;
cout << "Age: " << ptr->e_age << endl;
cout << "Degree: " << ptr->e_degree << endl;
}
getch();
return 0;
}
for (i=1; i<4; i++)Arrays start at index 0, not 1. You also haveptr = &e[3];The advice is to not try to simulate 1-based arrays in C++, as it is very easy to make off-by-one mistakes.