开发者

Syntax error with macro array access

开发者 https://www.devze.com 2023-01-24 12:06 出处:网络
I\'m att开发者_C百科empting to read values from a 2-dimensional array and multiply them to make a new array array. This isn\'t entirely important.

I'm att开发者_C百科empting 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.


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

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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消