Error trying to imprint the return of a two-dimensional matrix

Asked

Viewed 105 times

2

I’m trying to print a two-dimensional matrix through a loop for only that the matrix it is returned through a function that I created. I’m suspecting that the mistake must be found in it but I’m not managing to find the mistake

#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;
}

compiler points out that there is 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 don’t see anything wrong, where would be the error of this code? another thing is that the loop for within the function retorna_matriz2D() is executed but that of my function main() no, indicating that my matrix was assigned to my pointer *r.

2 answers

3


One thing I often say is that confusing codes that do more than they should always give room for error. 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.

#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]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Why is the value of mat2D 2 when you didn’t declare it? I never studied C, but I thought I knew the basics just by learning C#.

  • @bigown I want my matrix to be assigned to a variable in *r in my case and the loop to be executed based on the returned matrix, in your example the function returns

  • @Francis I don’t know if I understand what the doubt is, there may be something you don’t understand, but it seems to me that the code makes the obvious and the same in all normal languages

  • @Assanges does not return because it does not need, was redundant and caused problems. Don’t invent unnecessary things in code, make it simple that works best.

  • @bigown this output is possible only with the function ret_matriz2D() so it would not make sense, I need the matrix to be returned so I can use it outside the function.

  • 1

    @Assanges what you’re talking about doesn’t make sense.

  • @Assanges , the way it is done, the matrix information is properly fit to be manipulated. What happened is that the variable mat2D not modified, but rather the content it points to, as a side effect of the call to function retorna_matriz2D

  • @mustache why don’t you?

  • @Assanges you’re the one who has to make sense of what you’re talking about, it doesn’t because I showed you that simplifying everything works and it’s better, you want to complicate and stop working, it doesn’t make sense to me.

  • Really @bigown now I manage to understand your example and this much simpler what happens is that I was testing functions that return data I created an array[4] of integers and manage to return it to a *variable and then read through a loop for but this my matrix empenco do not know why, taking this your example is much simpler and functional thanks!

  • @bigown here is an example of how I implemented with an https://ideone.com/vCFytharray

  • 1

    For me this is wrong, although it works, but you have the right to do as you want, I will always teach the right.

Show 7 more comments

0

Let’s see what kind of r: int*. This means that it is only a pointer. A simple is pure pointer.

Pointers basically have two operations:

  1. De-referencing (operator *), which returns the type pointed;
  2. Pointer arithmetic/pointer offset (operator +), what returns a pointer.

There is also an operation responsible for offset is the dereferencing: element in the index i. This operation is denoted by [i] and is equivalent to *(ptr + i).

When you try to do 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 cannot suffer direct dereferencing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.