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
.
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#.
– Francisco
@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
– user45474
@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
– Maniero
@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.
– Maniero
@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.
– user45474
@Assanges what you’re talking about doesn’t make sense.
– Maniero
@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 functionretorna_matriz2D
– Jefferson Quesado
@mustache why don’t you?
– user45474
@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.
– Maniero
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!
– user45474
@bigown here is an example of how I implemented with an https://ideone.com/vCFytharray
– user45474
For me this is wrong, although it works, but you have the right to do as you want, I will always teach the right.
– Maniero