This program will scan a PGM file and store it's values to an array dynamically allocated img inside the function LerPGM(), the function will then return &img. As I declared PGM *imgconvI assign LerPGMto it (imgconv=LerPGM()). The thing is, the printfinside the function (printf("%d ", img.imagem[i][j]) works perfectly, but the printf("%d ", imgconv->imagem[i][j]) inside the main() only prints the first item and then the program stops working.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
    int c;
    int l;
    unsigned char maximo;
    unsigned char **imagem;
} PGM;
PGM *LerPGM(char* entrada);
int main()
{
    PGM *imgconv;
    int i, j;
    imgconv=LerPGM("entrada.pgm");
    for(i=0; i<imgconv->l; i++){
        for(j=0; j<imgconv->c; j++){
            printf("%d ", imgconv->imagem[i][j]);
        }
        printf("\n");
    }
    return 0;
}
PGM *LerPGM(char* entrada){
    PGM img;
    char tipo[3];
    int i, j;
    FILE *arq;
    arq = fopen(entrada, "r");
    if(arq == NULL){
        printf("Arquivo nao encontrado.");
        return 0;
    }
    fscanf(arq, "%s %d %d %d", &tipo, &img.c, &img.l, &img.maximo);
    if(strcmp(tipo, "P2")){
        printf("O arquivo nao e PGM.");
        return 0;
    }
    img.imagem = malloc(img.l * sizeof(char *));
    for(i=0; i<img.c; i++) img.imagem[i] = malloc(img.c * sizeof(char));
    if(img.imagem == NULL){
        printf("Falha na alocacao de memoria.");
        return 0;
    }
    for(i=0; i<img.l; i++){
        for(j=0; j<img.c; j++){
            fscanf(arq, "%d", &img.imagem[i][j]);
        }
    }
    fclose(arq);
    for(i=0; i<img.l; i++){
        for(j=0; j<img.c; j++){
            printf("%d ", img.imagem[i][j]);
        }
        printf("\n");
    }
    return &img;
}
 
     
    