# Thread: Parsing and returning array pointers?

1. This is kinda embrassing.

Printed out first member of normals, and it's fine. The problem seems to be with the calcNormal function, as the values aren't quite correct. Somehow got it into my head that calcNormal couldn't possible be wrong.

2. I have done some more playing around, and the problem is actually still here, and the problem is when transferring the contents of normArray to normals.

I had to make 'normals 2d', as if it is just 1d (without {0,0,0}), it will not compile at all.

Both printf's should print out 0,0,-1, but they differ. So, back to square one; why is 'normals' not settings its contents to what is returned by calcNormal?

Code:
```
float normArray[3];  // If declared in main, doesn't compile. This is the least of my worries at the moment.

int main(void)
{
GLfloat points[4][3] = { {0,0,0}, {0,1,0}, {1,1,0}, {1,0,0} };
GLfloat normals[2][3] = {{calcNormal(points[0], points[1], points[3])}, {0, 0, 0}};
printf("actual normal is %f, %f, %f\n", normals[0][0], normals[0][1], normals[0][2]);

return 0;
}

GLfloat calcNormal(GLfloat* B1, GLfloat* B2, GLfloat* T)
{
GLfloat A[3] = {B2[0] - B1[0], B2[1] - B1[1], B2[2] - B1[2]};
GLfloat B[3] = {T[0] - B1[0], T[1] - B1[1], T[2] - B1[2]};

normArray[0] = (A[1]*B[2]) - (A[2]*B[1]);
normArray[1] = (A[2]*B[0]) - (A[0]*B[2]);
normArray[2] = (A[0]*B[1]) - (A[1]*B[0]);

printf("normal is %f, %f, %f\n", normArray[0], normArray[1], normArray[2]);

return normArray[0];
}```

3. Shouldn't point[0], point[1], etc., have a second dimension?

It is a 2D array.

4. It does have a second dimension. Point[0] is (0,0,0)

5. Originally Posted by thealmightyone
It does have a second dimension. Point[0] is (0,0,0)
I was thinking you were going to say that. Doesn't that mean you're trying to put 9 numbers total, into the 0th row of the receiving array?

Then the second row, just gets three 0's, so it's okay.

6. That question confuses me, so will answer best I can. I am passing 3 arrays, of size 3, into calcNormal. calcNormal is recieving the arrays correctly, as it is correctly calculating values. I have verified this with different input arrays.

The issue is in passing the values in normArray, AFTER the calculations, into normals, via a pointer.

EDIT: Remember, normArray is a 1d array, not 2d, of size 3.

7. My compiler immediately gives the error:
"Illegal initialization", on that line.

I'm suggesting the normals array, is not getting the values you want it to have. You have 9 ints, trying to be initialized into one row which holds only 3 ints.

points[0] = 3 int's
points[1] = 3 int's
points[3] = 3 ints

All for the 0th row of the normals array, which holds 3 ints, only.

8. Oh, you think calcNormals returns an array of size 9?

Arrays point[0], [1] and [2] are fed INTO calcNormal (called by normals[x]), which returns pointer to normArray (size 3), which normals[x] is meant to set itself to.

9. No, I've been helping beginners too much.

normArray[0] is one number, however.

It's not a pointer to three numbers.

10. Well spotted. Not fixed the code, but 1 step there. Should be &normArray[0], and function should be defined to return float*. I still can't get it to compile though, am getting 'excess elements in scalar initializer' errors.

11. I made my numbArray global, as per your suggestion, and changed the return from calcNormal to void.

No joy. The compiler won't have anything to do with initializations that involve a row of digits, being subtracted by another row of digits, and the result of that, going into one element of the array being initialized.

My suggestion would be to initialize the arrays, then do the subtraction in for loops, and get those answers into variables, and then assign them to the array either with more for loops or explicit assignments.

I used that to get main() to compile, like this:

Code:
```#include <stdio.h>

float normArray[3];

void calcNormal(float* B1, float* B2, float* T);

int main(void)
{
int i;
float points[4][3] = { {0,0,0}, {0,1,0}, {1,1,0}, {1,0,0} };
float normals[2][3];

calcNormal(points[0], points[1], points[3]);

for(i = 0; i < 3; i++) {
normals[0][i] = normArray[i];
normals[1][i] = 0;
}
normals[0][2] = normArray[3];
printf("actual normal is %f, %f, %f\n", normals[0][0], normals[0][1], normals[0][2]);

i = getchar();
return 0;
}```
I'm not confident that code is correct, because I couldn't actually test it, but it does compile. That's how I'd work with the calcNormal() function.

Your way would be a great time saver, I couldn't grok the concept of it at first.

12. Yeah, I began coming to the realisation that what I wanted to isn't actually possible (I'm always looking for small and elegant solutions). Got it all working with some for loops now.