C++ pointer and struct

Hi all, I am a Computer Science student who just started C++. I am on struct right now. I got an error message but I don't have any idea.. It says "segmentation fault" . I looked up what kind of error message this is and it says "caused by memory location that doesn't belong to you ". So I assume my last for loop has syntactical error but I can't pinpoint what's wrong and why is wrong.
followings are my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


struct Course
{
    string name;
    int Idnum;
    int *test;
    double average;
    double grade;

};

int main()
{
    Course *data1=nullptr;
    
    int SIZE1;
    int SIZE2;
    cout<<"Enter the number of students in the group : ";
    cin>>SIZE1;

    data1 = new Course[SIZE1];

    cout<<"Enter the number of test done by these students : ";
    cin>> SIZE2;
 
    data1[SIZE1].test =nullptr;
    data1[SIZE1].test = new int[SIZE2];
 
    cin.ignore();


    for(int i=0; i<SIZE1; i++)
    {   
    cout<<"Enter the name of student :";
    getline(cin,data1[i].name);

    cout<<"Enter the ID number of this student : ";
    cin>>data1[i].Idnum;

        for(int j=0; j<SIZE2; j++)
        {
            cout<<"Enter test#"<<i+1<< ": ";
            cin>>data1[i].test[j];
    
        }




Here is where the madness begins:

1
2
    data1[SIZE1].test = nullptr;
    data1[SIZE1].test = new int[SIZE2];

There are two errors here.
Firstly, you are writing to offset SIZE1, which is one element beyond the end of the array. Remember that array offsets range from 0 to one less than the size.
Secondly, you need to assign this memory in a loop since you need to assign it to every student.

Also note, there's no need to set a pointer to nullptr just before you set it to something else.
Last edited on
As a starter, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string>
#include <iostream>

struct Course {
	std::string name;
	int Idnum {};
	int* test {};
	double average {};
	double grade {};
};

int main()
{
	size_t SIZE1 {};
	size_t SIZE2 {};

	std::cout << "Enter the number of students in the group : ";
	std::cin >> SIZE1;

	std::cout << "Enter the number of test done by these students : ";
	std::cin >> SIZE2;

	const auto data1 {new Course[SIZE1]{}};

	for (size_t i = 0; i < SIZE1; ++i) {
		std::cout << "Enter the name of student :";
		std::getline(std::cin >> std::ws, data1[i].name);

		std::cout << "Enter the ID number of this student : ";
		std::cin >> data1[i].Idnum;

		data1[i].test = new int[SIZE2] {};

		for (size_t j = 0; j < SIZE2; ++j) {
			std::cout << "Enter test#" << j + 1 << ": ";
			std::cin >> data1[i].test[j];
		}
	}

	for (size_t i = 0; i < SIZE1; ++i) {
		std::cout << data1[i].Idnum << '\t' << data1[i].name << '\t';

		for (size_t j = 0; j < SIZE2; ++j)
			std::cout << data1[i].test[j] << ' ';

		std::cout << '\n';
	}

	for (size_t i = 0; i < SIZE1; ++i)
		delete[] data1[i].test;

	delete[] data1;
}

Thank you so much, I changed the code according to your advice. It seems working well although I need to spend more time to understand what is going on by these pointers. I will leave my modified code here.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    for(int i=0; i<SIZE1; i++)
    {   
   
    data1[i].test = new int[SIZE2];   
    cout<<"Enter the name of student :";
    getline(cin,data1[i].name);

    cout<<"Enter the ID number of this student : ";
    cin>>data1[i].Idnum;

        for(int j=0; j<SIZE2; j++)
        {
            cout<<"Enter test#"<<j+1<< ": ";
            cin>>data1[i].test[j];
            total = total +data1[i].test[j];
        }
        data1[i].average = total/SIZE2;
        total =0; 




    cin.ignore();

    }   

Topic archived. No new replies allowed.