0

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;
     }
4
  • 1
    What exactly is the question? Commented Dec 11, 2014 at 19:51
  • the problem is when i run the code in the output the employee index changes but the data remain the same that is for employee 1... and i'm not sure about pointers i structures and arrays. Commented Dec 11, 2014 at 19:52
  • actually i need help in displaying the employee data using pointers... Commented Dec 11, 2014 at 19:53
  • @attiqa for (i=1; i<4; i++) Arrays start at index 0, not 1. You also have ptr = &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. Commented Dec 11, 2014 at 20:06

3 Answers 3

1

Your pointer is pointing to the third element of an array. Having that in mind, your for loop will print out the third element three times. You should pass your array identifier e(also a pointer) to "ptr", and then increment "ptr" in for loop. That's called pointer arithmetic.

More info: http://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm

Also, since C++ is OO language, i would recommend using classes instead of structures.

Sign up to request clarification or add additional context in comments.

Comments

0

Arrays start with index of 0, not 1. Pointer to the end is not helpful either, you want to point to the beginning of the array and increment each iteration through your print loop.

#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 = 0;
    cout << "Enter the Employee Data:" << endl;
    for (i=0; i<3; ++i)
    { 
       cout << "Data of Employee #" << (1+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 = e;
     for (i=0; i<3; ++i, ++ptr)
      {
       cout << e[i].e_name, e[i].e_id, e[i].e_age, e[i].e_degree;
       cout << "Data of Employee #" << (1+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;
     }

2 Comments

That's rich. Why -1?
thanks for saving me... i was like drowning in the sea of pointers.. thanks for your help... :)
0
 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;
 }

This code, probably, doesn't do what you expect. It's because ptr always points to the same thing (e[3]).

replace it with:

 struct employee *ptr;
 for (i=1; i<4; i++)
 {
   ptr = &e[i-1]; //i-1 because 0 is actually the first element
   .... //your code
 }

You can do the same thing by setting pointer to the first element of your array, and increase it in your loop.

2 Comments

how "0" is the first element the index is initialized by 1... so 1-1=0 and no data for i=0....
You are populating the array incorrectly. In C and C++ arrays start indexing with 0 not 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.