Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

opengl - C - invalid use of non-lvalue array

I have a matrix struct:

typedef struct Matrix
{
    float m[16];
} Matrix;

When I try to call this function:

memcpy(m->m, MultiplyMatrices(m, &translation).m, sizeof(m->m));

I get an error at compile time saying:

error: invalid use of non-lvalue array

MultiplyMatrices returns a Matrix.

I only get this error if I use gcc to compile the file into an object, if I use g++ to compile the object I get no error.

I am not even sure what the error means, I have a feeling it has to do with the array stored in the Matrix returned by MultiplyMatrices.

If you need to see more code let me know.

This code is from this tutorial: OpenGL Book Chapter 4

p.s. I would like to keep this code strict iso/ansi, if there is no other solution however, then I'll just have to deal with it.

EDIT: I ended up going with creating a temporary Matrix then copying the array.

Matrix tempMatrix;

...

tempMatrix = MultiplyMatrices(m, &translation);
memcpy(m->m, tempMatrix.m, sizeof(m->m));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The return value of MultiplyMatrices() is not an lvalue (like the return value of any function), which means that you can't take its address. Evaluating an array (including an array member of a structure) implicitly takes the address of the first element, so you can't do that.

You can, however, use simple assignment of the containing struct:

*m = MultiplyMatrices(m, &translation);

As long as your struct only contains the one element as you have shown, this is exactly the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...