Home > Software design >  i made c code where its need to pass structure pointer in a function
i made c code where its need to pass structure pointer in a function

Time:11-23

i got confused about the structure when i need to to pass the value in a function

#include <iostream>
using namespace std;

struct student
{
    int studentID;
    char studentName[30];
    char nickname[10];
};

void read_student(struct student *);
void display_student(struct student *);

int main()
{

    student *s;
    //struct student *ps;
    read_student(s);

    display_student(s);

}

void read_student(struct student *ps)
{
    int i;
    for (i = 0; i <= 2; i  )
    {
        cout << "Enter the studentID : ";
        cin >> ps->studentID;
        cout << "Enter the full name :";
        cin.ignore();
        cin >> ps->studentName;
        cout << "Enter the nickname : ";
        cin.ignore();
        cin >> ps->nickname;
        cout << endl;

    }
}

void display_student(struct student *ps)
{
    int i;
    cout << "student details :" << endl;

    for (i = 0; i <= 2; i  )
    {
        cout << "student name : " << *ps ->studentName << " (" << &ps ->studentID << ")" << endl;

        ps  ;
    }
}

its only problem at the variable at line 19 and when i try to edit it will became more problem

 student *s;
 //struct student *ps;

  //struct student *ps;
  read_student(s);
  display_student(s);

also can someone explain how to transfer pointer value of structure to the function and return it back to the main function

CodePudding user response:

You are suffering from some leftovers from your C-Language time. And, you have still not understood, how pointer work.

A pointer (as its name says) points to something. In your main, you define a student pointer. But it is not initialized. It points to somewhere. If you read data from somewhere, then it is undefined behavior. For writing, it is the same, plus that the system will most likely crash.

So, define a student. And if you want to give it to a sub-function, take its address with th &-operator (this address will point to the student) and then it will work.

You need also to learn abaout dereferencing. Your output statement is wrong.

Last but not least, if you want to store 3 students, then you need an array or some other storage where to put them . . .

In your read function you always overwrite the previously read student and in your display function you show undetermined data.

Please have a look at your minimal corrected code:

#include <iostream>
using namespace std;

struct student
{
    int studentID;
    char studentName[30];
    char nickname[10];
};

void read_student( student*);
void display_student( student*);

int main()
{
    student s;
    //struct student *ps;
    read_student(&s);

    display_student(&s);

}

void read_student( student* ps)
{
        cout << "Enter the studentID : ";
        cin >> ps->studentID;
        cout << "Enter the full name :";
        cin.ignore();
        cin >> ps->studentName;
        cout << "Enter the nickname : ";
        cin.ignore();
        cin >> ps->nickname;
        cout << endl;
}

void display_student( student* ps)
{
    cout << "student details :" << endl;
    cout << "student name : " << ps->studentName << " (" << ps->studentID << ")" << endl;
    ps  ;
}

And, the commenters are right. You must read a book.

  • Related