Pointer/Array and Struct Problems

Hello @all,
I am new to c++ so also to pointers/arrays and structs. I thought I understood them but now I've got some problems.

I'm about to write an OpenGL project with a 3D-Camera.
I used a struct for the cam and implemented some initialisation methods:

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
typedef struct Camera {
	float position[3];
	float upVector[3];
	float lookAtVector[3];
	float perpendicular[3];
} Camera;

float* crossProduct(float* v1, float* v2) {
	float result[3];
	result[0] = v1[1]*v2[2] - v1[2]*v2[1];
	result[1] = v1[2]*v2[0] - v1[0]*v2[2];
	result[2] = v1[0]*v2[1] - v1[1]*v2[0];
	return result;
}

void calculatePerpendicular(Camera *c) {
	Camera cam = *c;
	float* perpendicular = crossProduct(cam.upVector, cam.lookAtVector);
	cam.perpendicular[0] = perpendicular[0];
	cam.perpendicular[1] = perpendicular[1];
	cam.perpendicular[2] = perpendicular[2];
}

void initCamera(Camera cam) {
	//Camera cam = *c;
	cam.position[0] = 0.0f;
	cam.position[1] = 0.0f;
	cam.position[2] = 0.0f;

	cam.lookAtVector[0] = 0.0f;
	cam.lookAtVector[1] = 0.0f;
	cam.lookAtVector[2] = 1.0f;

	cam.upVector[0] = 0.0f;
	cam.upVector[1] = 1.0f;
	cam.upVector[2] = 0.0f;

	calculatePerpendicular(&cam);
}


now in my main.cpp I deklared the cam and wanted to initialize it but all values stay at their default value 0.0f

1
2
3
struct Camera cam;//global

initCamera(cam);//in main 


well I guess I just messed up something with the pointers because when I debugged
1
2
3
	cam.lookAtVector[0] = 0.0f;
	cam.lookAtVector[1] = 0.0f;
	cam.lookAtVector[2] = 1.0f;

worked but it wasnt save in the object i wanted to use in the prog. Also calculatePerpendicular(&cam); didn't work.

I wanted to learn these things so I used a camera object as parameter for initialisation and a pointer to the camera for the perpendicular.

I hope the problem is clear.

Greetings,
jan
Last edited on
pass as reference, because this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void initCamera(Camera cam) {
	//Camera cam = *c;
	cam.position[0] = 0.0f;
	cam.position[1] = 0.0f;
	cam.position[2] = 0.0f;

	cam.lookAtVector[0] = 0.0f;
	cam.lookAtVector[1] = 0.0f;
	cam.lookAtVector[2] = 1.0f;

	cam.upVector[0] = 0.0f;
	cam.upVector[1] = 1.0f;
	cam.upVector[2] = 0.0f;

	calculatePerpendicular(&cam);
}

does nothing.
Last edited on
yeah this was my first thought too as you can see in my comment :) but its just the same.
calculatePerpendicular(&cam); doesnt set the perpendicular either.
you can also write it in the constructor
Do you know what "Pass as reference" Means? Because it's almost impossible that the results are the same.
well i used a reference here: calculatePerpendicular(&cam) didn't I?

u mean this right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void initCamera(Camera *c) {
	Camera cam = *c;
	cam.position[0] = 0.0f;
	cam.position[1] = 0.0f;
	cam.position[2] = 0.0f;

	cam.lookAtVector[0] = 0.0f;
	cam.lookAtVector[1] = 0.0f;
	cam.lookAtVector[2] = 1.0f;

	cam.upVector[0] = 0.0f;
	cam.upVector[1] = 1.0f;
	cam.upVector[2] = 0.0f;

	calculatePerpendicular(c);
}

Last edited on
ah i thought 'Camera *c' is a reference to the cam but its actually 'Camera &c'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void calculatePerpendicular(Camera &c) {
	float* perpendicular = crossProduct(c.upVector, c.lookAtVector);
	c.perpendicular[0] = perpendicular[0];
	c.perpendicular[1] = perpendicular[1];
	c.perpendicular[2] = perpendicular[2];
}

void initCamera(Camera &c) {
	c.position[0] = 0.0f;
	c.position[1] = 0.0f;
	c.position[2] = 0.0f;

	c.lookAtVector[0] = 0.0f;
	c.lookAtVector[1] = 0.0f;
	c.lookAtVector[2] = 1.0f;

	c.upVector[0] = 0.0f;
	c.upVector[1] = 1.0f;
	c.upVector[2] = 0.0f;

	calculatePerpendicular(c);
}


ok solved this problem: Thanks Darkmaster and EssGeEich
finally better understanding of pointers :)
Last edited on
Yeah, the last example is the pass-by-reference.
The '*' simbol means passing a pointer.
Where there is none of them, it means passing by value.
Topic archived. No new replies allowed.