宏数组访问时出现语法错误

时间:2021-01-21 22:44:05

I'm attempting to read values from a 2-dimensional array and multiply them to make a new array array. This isn't entirely important.

我试图从二维数组中读取值并将它们相乘以生成一个新的数组数组。这并不完全重要。

I have created a macro to read the values instead of a function to theoretically be more efficient, but I'm having a syntax error that I can't figure out. The line of issue is

我创建了一个宏来读取值而不是函数从理论上讲更有效,但是我遇到了一个我无法弄清楚的语法错误。问题在于

    // compute and write the value for the result array
        writearr( result, n, r, c, ( READ(r, c, A*) * READ(c, r, A*) ) );

with function header

带有函数头

    void newarr(int n, int* A, int* result)

The macro is

宏是

    #define READ(a, b, arr) (arr[a][b])

and when I try to compile this I get

当我尝试编译这个时,我得到了

    gcc -Wall -O2   -c -o placeholder.o placeholder.c
    placeholder.c: In function âwritearrâ:
    placeholder.c:26: error: expected expression before â[â token
    make: *** [placeholder.o] Error 1

but I can't quite figure out what the issue is.

但我无法弄清问题是什么。

2 个解决方案

#1


4  

First of all, you need to enclose your macro arguments in parentheses.

首先,您需要将宏参数括在括号中。

#define READ(a, b, arr) ((arr)[a][b])

Second, you should use A instead of A* for dereferencing. A* is not valid at all, but you wanted perhaps &A (which as actually incorrect as well)?

其次,您应该使用A代替A *进行解除引用。 A *根本没有效果,但是你想要和A(实际上也不正确)?

Third, in this case the macro doesn't actually bring any advantage against just accessing the array.

第三,在这种情况下,宏实际上并没有带来任何优势,只是访问阵列。

Fourth, you declared A as a one-dimentional array, you cannot use it as a multidimentional one. Taking an address of a single-dimensional array doesn't allow you to switch to the "next" row automatically, as C++ doesn't know how large the row is going to be.

第四,你将A声明为一维数组,不能将它用作多维数组。获取一维数组的地址不允许您自动切换到“下一行”,因为C ++不知道该行的大小。

#2


0  

I don't see the point of using READ macro here. If you have to user this semantics, you need to do:

我没有看到在这里使用READ宏的意义。如果必须使用此语义,则需要执行以下操作:

writearr( result, n, r, c, ( READ(r, c, A) * READ(c, r, A) ) );

#1


4  

First of all, you need to enclose your macro arguments in parentheses.

首先,您需要将宏参数括在括号中。

#define READ(a, b, arr) ((arr)[a][b])

Second, you should use A instead of A* for dereferencing. A* is not valid at all, but you wanted perhaps &A (which as actually incorrect as well)?

其次,您应该使用A代替A *进行解除引用。 A *根本没有效果,但是你想要和A(实际上也不正确)?

Third, in this case the macro doesn't actually bring any advantage against just accessing the array.

第三,在这种情况下,宏实际上并没有带来任何优势,只是访问阵列。

Fourth, you declared A as a one-dimentional array, you cannot use it as a multidimentional one. Taking an address of a single-dimensional array doesn't allow you to switch to the "next" row automatically, as C++ doesn't know how large the row is going to be.

第四,你将A声明为一维数组,不能将它用作多维数组。获取一维数组的地址不允许您自动切换到“下一行”,因为C ++不知道该行的大小。

#2


0  

I don't see the point of using READ macro here. If you have to user this semantics, you need to do:

我没有看到在这里使用READ宏的意义。如果必须使用此语义,则需要执行以下操作:

writearr( result, n, r, c, ( READ(r, c, A) * READ(c, r, A) ) );