Error trying to print the return of a two-dimensional array

2

I'm trying to print a two-dimensional array through in a loop for only the array it's returned through a function that I've created. I'm suspecting that the error should be in it but I can not find the error

#include <stdio.h>
#define DIM 2

int *retorna_matriz2D();

int mat2D[DIM][DIM];

int main(){
    int x, y;
    int *r = retorna_matriz2D(mat2D);
    for(x = 0; x < DIM; x++){
        for(y = 0; y < DIM; y++){
            printf("%d", r[x][y]); // LINHA 13
            printf("\n");
        }
    }
    return 0;
}

int *retorna_matriz2D(int mat[][DIM]){
    int x, y;
    for(x = 0; x < DIM; x++){
        for(y = 0; y < DIM; y++){
            mat2D[x][y] = 2;
        }
    }
    for(x = 0; x < DIM; x++){
        for(y = 0; y < DIM; y++){
        printf("%d", mat[x][y]);
        printf("\n");
        }
    }
    return *mat;
}

The compiler points out that it has an error on line 13 with the message

  

subscripted value is neither array nor pointer nor vector

supposedly indicating that the error is in the line of printf() but I do not see anything wrong, where would be the error of this code? another thing is also that the for loop within the retorna_matriz2D() function is executed but that of my main() function, not indicating that my array was assigned to my *r pointer.

    
asked by anonymous 09.09.2017 / 23:21

2 answers

3

One thing I often say is that confusing codes that do more than they should always give room for mistakes. This is the case. This code can be greatly simplified and solved some other problems not so visible, and done this there is no error whatsoever.

#include <stdio.h>
#define DIM 2

void retorna_matriz2D(int mat[][DIM]) {
    for (int x = 0; x < DIM; x++) {
        for (int y = 0; y < DIM; y++) {
            mat[x][y] = 2;
            printf("%d\n", mat[x][y]);
        }
    }
}

int main(){
    int mat2D[DIM][DIM];
    retorna_matriz2D(mat2D);
    for (int x = 0; x < DIM; x++) {
        for (int y = 0; y < DIM; y++) {
            printf("%d\n", mat2D[x][y]);
        }
    }
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

    
09.09.2017 / 23:39
0

Let's look at the type of r : int* . That means it's just a pointer. A simple pointer is pure.

Pointers basically have two operations:

  • Dereferencing ( * operator), which returns the type pointed to;
  • Pointer arithmetic / pointer offset ( + operator), which returns a pointer.
  • There is also an operation responsible for making the offset is the dereferencing: element at index i . This operation is denoted by [i] and is equivalent to *(ptr + i) .

    When you try to r[i][j] , let's see how each operation is done individually.

    Initially, r[i] . This is equivalent to *(r + i) , therefore returning an integer. We call it v . So we have r[i][j] == v[j] . However v is of the type referenced by r , therefore a int . And integers can not undergo direct dereferencing.

        
    09.09.2017 / 23:47