C - 非左值数组的无效使用 [英] C - invalid use of non-lvalue array

查看:27
本文介绍了C - 非左值数组的无效使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵结构:

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 返回一个矩阵.

MultiplyMatrices returns a Matrix.

如果我使用 gcc 将文件编译为对象,我只会收到此错误,如果我使用 g++ 编译对象,我不会出错.

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.

我什至不确定错误是什么意思,我感觉它与存储在 MultiplyMatrices 返回的矩阵中的数组有关.

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.

此代码来自本教程:OpenGL 书籍第 4 章

附言我想保持这个代码严格的iso/ansi,但是如果没有其他解决方案,那么我只需要处理它.

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.

我最终创建了一个临时矩阵,然后复制了数组.

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));

推荐答案

MultiplyMatrices() 的返回值不是左值(就像任何函数的返回值一样),这意味着你可以不要取它的地址.对数组(包括结构的数组成员)求值会隐式获取第一个元素的地址,因此您不能这样做.

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.

但是,您可以使用包含 struct 的简单赋值:

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

*m = MultiplyMatrices(m, &translation);

只要你的 struct 只包含你所展示的一个元素,这就完全一样了.

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

这篇关于C - 非左值数组的无效使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆