Pointers to structure

Dec 2, 2015 at 7:17pm
Hi, I have my structure. I would like to create a function where I use pointers.
I created one but It's not working as it's showing the memory but not the actual data...

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

struct Evaluation
{
	float Projet;
	float Intra;
	float Final;
	int noteFinale;
};

struct Student
{
	int ID;
	string firstname;
	string name;
	Evaluation grade;

	};

void displayStudent(Student tabStudent[])
{
	Student stu1;
	Student *stuPtr = &stu1;
	(*stuPtr).ID;
	(*stuPtr).firstname;
	(*stuPtr).name;
	(*stuPtr).grade.Projet;
	(*stuPtr).grade.Intra;
	(*stuPtr).grade.Final;
	(*stuPtr).grade.noteFinale;

	cout << "\n" << setw(20) << (*stuPtr).ID << setw(10) << (*stuPtr).firstname<< setw(20) << (*stuPtr).name<< setw(20) << (*stuPtr).grade.Projet	<< setw(20) <<(*stuPtr).grade.Intra << setw(20) << (*stuPtr).grade.Final << setw(20) << (*stuPtr).grade.noteFinale;
	
Dec 2, 2015 at 7:38pm
Lines 23 through 29 look bogus to me. The correct syntax to dereference a pointer is something like this:

1
2
3

cout << "\n" << setw( 20 ) << stuPtr->ID << 
Dec 2, 2015 at 7:50pm
koothkeeper wrote:
Lines 23 through 29 look bogus to me.

There is nothing wrong with the syntax. Dereferening the pointer first, then making a dot reference to a member is perfectly fine.

However, what is bogus is that lines 23-29 do nothing. No values are assigned to the members. Since there is no constructor, the member values will be garbage.
Dec 2, 2015 at 8:02pm
But the values are assigned in another function which is ''create student''.

The user input the data, which goes into the structure
and then i want to use this function to display the datas

The datas are in tabStudent[]
Last edited on Dec 2, 2015 at 8:07pm
Dec 2, 2015 at 8:38pm
But the values are assigned in another function which is ''create student''.

Since you didn't show us create_student, we had no idea it existed. That still doesn't change the fact that lines 23-29 do nothing.

Line 19: You're passing tabStudent as an argument, but make no reference to it. So if tabStudent has been initialized with values, you're not using those values.

Line 21: Stu1 is an uninitialized struct. As stated before, it contains garbage. You're going to print garbage at line 31.
Last edited on Dec 2, 2015 at 8:42pm
Topic archived. No new replies allowed.