0

I have a problem in writting a 2D array using pointers. It works fine without splitting the code in functions but making a function to print the array (leer_array) something goes bad.

This works:

#include <stdio.h>
int main ()
{
    int f=2,c=3, count=0;
    int i,j;
    int a[3][3]={{1,2,3}, 
                 {4,5,6},
                 {7,8,9}};

    for(i=0;i<f;i++)
    {
        for(j=0;j<c;j++)
            printf("%i ",*(*(a+i)+j));
        printf("\n");
    }


    return 0;
}

But the same using a function does not:

#include <stdio.h>

void leer_array(int **v,int f,int c)
{
    int i,j;
    for(i=0;i<f;i++)
    {
        for(j=0;j<c;j++)
            printf("%i ",*(*(v+i)+j));
        printf("\n");
    }
}


int main ()
{
    int f=2,c=3, count=0;
    int i,j;
    int a[3][3]={{1,2,3}, //Inicialización de matriz
                 {4,5,6},
                 {7,8,9}};

    leer_array(a,f,c);


    return 0;
}

Thank you!!

0 Answers0