Why I can't get the name through the pointer to the element before the last one?

0

I want to get all names and print them. How to fill the array of structure and then print data from each element in the array?

I can't even get the name before the last one element. . The main problem in a char* because with string it works. But I need to do it without strings. It gives me this error:

Exception thrown at 0x0FADF6BC (ucrtbased.dll) in Vuz.exe: 0xC0000005: Access violation reading location 0x00000001

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#define SIZE 2
using namespace std;

struct Student {
    const char* name;
    const char* surname;
    const char* major;
    int group;

    Student(){}

    Student(const char* name, const char* surname, const char* major, int grp)
    {
        this->name = name;
        this->surname = surname;
        this->major = major;
        this->group = grp;
    }

};


int main()
{
    Student* st = new Student[SIZE];

    for (int i = 0; i < SIZE; i++)
    {
        char name[30];
        char surname[30];
        char major[10];
        int group;

        cout << "Enter name, surname, major and group of new student" << endl;
        cin >> name >> surname >> major >> group;

        st = new Student(name, surname, major, group);
        *st++;
    }
    *st--;
    *st--;//Here I have a problem
    cout << st->name << endl;

    return 0;
}

I want to fill this array correctly(using char* ) and print all these names.

c++
struct
char
dynamic-memory-allocation
asked on Stack Overflow May 12, 2019 by Little Max

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0