How to dynamically allocate an array using a pointer pointer in a void function

0

I'm trying to allocate an array in a void function, by sending a pointer pointer int **sigma , as lerArquivo(char *alfabeto, int *Q, int *Q0, int *F, int ***sigma, FILE *arq); , but I'm having difficulty allocating memory space in the function. I am sending the parameter in the wrong way because it is a pointer pointer or am I just wrong at the time of memory allocation?

Thank you in advance.

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

int buscaArquivo(FILE **arq);
void lerArquivo(char *alfabeto, int *Q, int *Q0, int *F, int ***sigma, FILE *arq);

int main(int argc, char *argv[]) {

    char alfabeto[10], **palavras;
    int Q, Q0, F, T, z, i; 
    FILE *arq;  
    int **sigma;



    if(buscaArquivo(&arq) == 0){
        lerArquivo(alfabeto, &Q, &Q0, &F, &sigma, arq);
        printf("\n");
        printf("\n");
        for(z=0; z< Q; z++){
            printf("teste");
            for(i=0; i< strlen(alfabeto); i++){
                printf("%d ",sigma[z][z]);
            }
            printf("\n");
        }
    }
    else
        printf("Arquivo não encontrado!");


    return 0;
}


int buscaArquivo(FILE **arq){

    char nome[50];

    printf("Nome do arquivo: ");
    scanf("%s", nome);

    *arq = (fopen(nome,"r"));

    if(*arq == NULL)
        return 1;
    else 
        return 0;   
}

void lerArquivo(char *alfabeto, int *Q, int *Q0, int *F, int ***sigma, FILE *arq){

    int i, j,x, y, z, N, T, tm;

    fscanf(arq,"%s", alfabeto);
    fscanf(arq,"%d", &(*Q));
    fscanf(arq,"%d", &(*Q0));
    fscanf(arq,"%d", &(*F));
    fscanf(arq,"%d", &N);
    tm = strlen(alfabeto);



    //Alocao da matriz sigma
    **sigma = (int****) malloc(*Q * sizeof(int***));
    for(z=0; z< *Q; z++){
        **sigma[z] = (int**) malloc(tm * sizeof(int*));
        for(i=0; i<tm; i++){
            sigma[z][i]=0;
            printf("%d ",sigma[z][i]);
        }
        printf("\n");
    }
}
    
asked by anonymous 17.03.2018 / 16:57

1 answer

0

Looking at the compiler warnings we see that the <string.h> library is missing, which is where it is declared from strlen .

Looking at the memory allocations you made:

void lerArquivo(char *alfabeto, int *Q, int *Q0, int *F, int ***sigma, FILE *arq){
    ...
    //Alocao da matriz sigma
    **sigma = (int****) malloc(*Q * sizeof(int***));

If sigma is a double pointer passed by main, doing **sigma = algo will not make sense. And less sense will capture a 4th level pointer on a 2nd level pointer. 4-level pointer ° because it is the cast you are doing with:

(int****)

So what you're looking for is something like:

*sigma = (int**) malloc(*Q * sizeof(int*));

Notice that I'm saving the% allocation of an array of sigma or a int** by pointing to int* (a int** ). You can always try to look only at the guys on each side and see if they play:

*sigma = int** //olhando apenas para o tipo da direita
*(int***) = int** //colocando o tipo de sigma
int** = int** //resolvendo os parentesis da esquerda
    
17.03.2018 / 22:38