Pass array as pointer

3

I need to make the proposal:

  

Construct a function that takes parameters from a pointer to an array, the number of rows and columns, and prints the array elements.

But I'm learning pointers and having difficulties, I've tried in several ways to pass the matrix pointer, but it's all wrong. Now the error is on the line:

            printf("\t%d",*mtr[lin][col]);
[Error] invalid types 'int* (*)[4][int*]' for array subscript

Here's my code:

#include <stdio.h>
#include <stdlib.h>

int matriz_ponteiro(int *mtr[3][4], int *lin, int *col){
    // imprimir a matriz
    for (*lin=0; *lin<3; *lin++){
        printf("\n");
        for (*col=0; *col<4; *col++){
            printf("\t%d",*mtr[lin][col]);
        }   
    }
    return *mtr[lin][col];

}


int main (){
    int res, 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++;
        }
    }
    res = matriz_ponteiro(mtr[3][4], lin, col);
}
    
asked by anonymous 15.11.2016 / 02:34

2 answers

2

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

    
15.11.2016 / 02:51
1

This whole code does not make much sense and at various points does not do what you think. It can be simplified, organized and modernized like this, there it works:

#include <stdio.h>
#include <stdlib.h>

void matriz_ponteiro(int mtr[3][4]) {
     for (int lin = 0; lin < 3; lin++) {
        for (int col = 0; col < 4; col++) {
             printf("\t%d", mtr[lin][col]);
        }   
        printf("\n");
    }
}

int main () {
    int mtr[3][4];
    int cont = 0;
    for (int lin = 0; lin < 3; lin++) {
        for (int col = 0; col < 4; col++) {
             mtr[lin][col] = cont++;
        }
    }
    matriz_ponteiro(mtr);
}

See working on ideone and on CodingGround .

There may be some other purpose, but this is not clear in the question, what you can infer is there. If you have specific questions you can ask. Learning to program in trial and error is not the most productive way, nor does it usually work very well.

Using pointers:

#include <stdio.h>
#include <stdlib.h>
#define LINHA 3
#define COLUNA 4

void matriz_ponteiro(int **mtr, size_t linhas, size_t colunas) {
     for (int lin = 0; lin < linhas; lin++) {
        for (int col = 0; col < colunas; col++) {
             printf("\t%d", mtr[lin][col]);
        }   
        printf("\n");
    }
}

int main () {
    int **mtr = malloc(LINHA * sizeof(*mtr));
    int cont = 0;
    for (int lin = 0; lin < LINHA; lin++) {
        mtr[lin] = malloc(COLUNA * sizeof(*mtr[lin]));
        for (int col = 0; col < COLUNA; col++) {
             mtr[lin][col] = cont++;
        }
    }
    matriz_ponteiro(mtr, LINHA, COLUNA);
}

See working on ideone and on CodingGround .

    
15.11.2016 / 02:48