I understand the code - first alternative
Look at this line:
matriz_ponteiro(mtr[3][4], lin, col);
lin
and col
are of type int
. mtr
is a 3x4 array of type int
, and therefore mtr[3]
is a 4-position array of type int
and finally mtr[3][4]
is int
. That is, the three parameters you pass are int
s.
In the function definition, we have this:
int matriz_ponteiro(int *mtr[3][4], int *lin, int *col){
That is, the parameters are of the " pointer types for 3x4 array of int
", " pointer to int
" and " pointer to int
". That is, they do not match what you use to call it down.
Well, in your code you mix arrays, pointers, and integers more or less at random, which demonstrates that you should not know what you're doing right, so let's take it easy. Apparently, the purpose of your matriz_ponteiro
function is to print the array. So here's the header:
void matriz_ponteiro(int mtr[3][4])
Why? Because it receives a 3x4 array of integers to print ( int mtr[3][4]
and returns no result ( void
).
Parameters lin
and col
are not required because it will go through all rows and columns. So, the implementation looks like this:
void matriz_ponteiro(int mtr[3][4]) {
int lin, col;
for (lin = 0; lin < 3; lin++) {
printf("\n");
for (col = 0; col < 4; col++) {
printf("\t%d", mtr[lin][col]);
}
}
}
And with that, in its main
, instead:
res = matriz_ponteiro(mtr[3][4], lin, col);
Use this:
matriz_ponteiro(mtr);
And you can also delete the variable res
.
Here's what your complete code looks like:
#include <stdio.h>
#include <stdlib.h>
void matriz_ponteiro(int mtr[3][4]) {
int lin, col;
for (lin = 0; lin < 3; lin++) {
printf("\n");
for (col = 0; col < 4; col++) {
printf("\t%d", mtr[lin][col]);
}
}
}
int main() {
int mtr[3][4];
int lin, col, cont;
cont = 0;
// armazenar o valor de cont em cada posição da matriz
for (lin = 0; lin < 3; lin++) {
for (col = 0; col < 4; col++) {
mtr[lin][col]= cont++;
}
}
matriz_ponteiro(mtr);
}
See here working on ideone.
Second alternative
If you have to have the number of rows and columns as a parameter, you can do this:
#include <stdio.h>
#include <stdlib.h>
void matriz_ponteiro(int linhas, int colunas, int matriz[linhas][colunas]) {
int lin, col;
for (lin = 0; lin < linhas; lin++) {
for (col = 0; col < colunas; col++) {
printf("\t%d", matriz[lin][col]);
}
printf("\n");
}
}
int main() {
int mtr[3][4];
int lin, col, cont;
cont = 0;
// armazenar o valor de cont em cada posição da matriz
for (lin = 0; lin < 3; lin++) {
for (col = 0; col < 4; col++) {
mtr[lin][col] = cont++;
}
}
matriz_ponteiro(3, 4, mtr);
}
See here working on ideone. It is important to note that the array has to be the last parameter, because the definition of it depends on the number of rows and columns that have to be declared before (that is, in the previous parameters).
The matrix used there is a pointer. To prove this, try putting at the end of the matriz_ponteiro
function, this:
mtr[0][0] = 1234;
And at the end of main
, use this twice:
matriz_ponteiro(3, 4, mtr);
And you will realize that the fact matrix is changed from within matriz_ponteiro
. If it was just copied, 1234 would not appear when calling the matriz_ponteiro
function again. This is only possible because a reference passage occurred, not just values. And if a reference passage occurred in C, it is because there was a pointer passing.
Third alternative
However, if you want the matriz_ponteiro
q function to receive an explicit pointer, you can do so:
#include <stdio.h>
#include <stdlib.h>
void matriz_ponteiro(int *matriz, int linhas, int colunas) {
int lin, col;
for (lin = 0; lin < linhas; lin++) {
if (lin != 0) printf("\n");
for (col = 0; col < colunas; col++) {
printf("\t%d", matriz[lin * colunas + col]);
}
}
}
int main() {
int mtr[3][4];
int lin, col, cont;
cont = 0;
// armazenar o valor de cont em cada posição da matriz
for (lin = 0; lin < 3; lin++) {
for (col = 0; col < 4; col++) {
mtr[lin][col] = cont++;
}
}
matriz_ponteiro(mtr[0], 3, 4);
}
See working on ideone.
This matriz[lin * colunas + col]
needs a better explanation. What happens is that the elements of the matrix are placed in the shape of one line after the other. That is, the array consists of a sequence of rows and each row has a number of elements equal to the number of columns. This can also be interpreted as an array with linhas * colunas
elements. This lin * colunas + col
formula accesses the desired element if you are using this form of indexing.
The use of mtr[0]
at the end is because the type must be int *
, not int **
. Another way would be to cast explicitly too (use (int *) mtr
).