Struct variable not remaining

I have the below code and I am trying to simply assign an integer (for testing purposes) to a variable inside a struct, which itself is inside of a class. I am not sure what I am doing wrong, but it seems that I am getting a new instance of the struct every time I try and access it from the same (already declared) class.

I feel like I am missing something obvious.

Anyways, in the program output, I entered the number '1337' and you can see that before I call the function to store it, it has been initialized to 0, but after it simply resets the value to 0.

Hopefully my question makes sense, or will be clear from the output. Thanks!


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
struct process
{
	int pid;
};

struct queue
{
	int number_in_queue;
	process procs[MAX_QUEUE_SIZE];
	
	queue() { number_in_queue = 0; }
};

class scheduler
{
	private:
		queue start;
		process CPU;
	public:
		void getInput();
		void addToQueue(queue, int);
};

int main()
{
	scheduler sched;
	sched.getInput();
	int clock = 0;
	while (1) {
		break;
	}
	return 0;
}

void scheduler::getInput()
{
	int temp;
	while(!cin.eof())
	{
		cin >> temp;
		cout << "1start.number_in_queue = " << start.number_in_queue << endl;
		addToQueue(start, temp);
		cout << "4start.number_in_queue = " << start.number_in_queue << endl;
	}
}

void scheduler::addToQueue(queue q, int temp)
{
	cout << "2q.number_in_queue = " << q.number_in_queue << endl;
	q.number_in_queue = temp;
	cout << "3q.number_in_queue = " << q.number_in_queue << endl;
}



./a.out
1337
1start.number_in_queue = 0
2q.number_in_queue = 0
3q.number_in_queue = 1337
4start.number_in_queue = 0
1338
1start.number_in_queue = 0
2q.number_in_queue = 0
3q.number_in_queue = 1338
4start.number_in_queue = 0
closed account (zb0S216C)
"scheduler::addToQueue( )" is where you're going wrong. The first formal parameter declaration states that the actual parameter that will be given to it will be copied. Therefore, the first formal parameter of "scheduler::addToQueue( )" will be working with a copy, and the changes made to the actual parameter will be local to the actual parameter. For instance:

1
2
3
4
5
6
7
8
9
10
void function(int param)
{
    param += 2;
}

int main( )
{
    int value(0);
    function(value);
}

In this code, "function( )" accepts an "int" as an actual parameter. During the call to "function( )", "value" is passed to it; "value" is in fact copied into "param" of "function( )". Changes to "param" will affect only "param" and not "value". This is known as passing-by-value.

To overcome this, we use references:

1
2
3
4
5
6
7
8
9
10
int function(int &param)
{
    param += 2;
}

int main( )
{
    int value(0);
    function(value);
}

In this code, "value" is passed-by-reference. When "function( )" is called, "param" of "function( )" will refer to the actual parameter ["value"]. Any changes made to "param" will affect "value".

See here for more: http://www.cplusplus.com/doc/tutorial/pointers/#reference

Wazzak
Last edited on
I knew it was something like that, I assumed it would pass the struct by reference (as like an array).

Thank you! Perfect response!
Topic archived. No new replies allowed.