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

Asked

Viewed 421 times

0

I’m trying to allocate from were dynamic a matrix into a function void, this by sending a pointer pointer as parameter int **sigma, as follows lerArquivo(char *alfabeto, int *Q, int *Q0, int *F, int ***sigma, FILE *arq);, but I’m having trouble allocating memory space in function. I am sending the parameter incorrectly because it is pointer pointer or I am just missing at the time of memory allocation?

Grateful from now on.

#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");
    }
}

1 answer

1

Looking at the compiler warnings we see that missing include library <string.h> which is where the strlen.

Analyzing a little 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 the main, do **sigma = algo does not make sense. And less sense will capture a pointer in 4th level on a 2nd level pointer. 4 level pointer as it is the cast what are you doing with:

(int****)

So what you’re looking for is something like:

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

Notice I’m holding on to the note by sigma (one int**) the allocation result of an array of int* namely a int**. You can always just try to look 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
  • sorry, had edited the code to be able to ask the question here, and by accident the function call was not edited.

  • @jaojpaulo If that’s the case everything I said except the first part of the error remains valid. I already edited the answer removing what does not apply.

  • Okay, thanks for the explanation anyway.

  • the way you explained when I would allocate an array with this double pointer I would do it right *sigma[i] = (int) malloc(tm * sizeof(int));

  • @jaojpaulo It actually depends a little. In your example in the allocation part sigma is a triple pointer, and as you have to modify what you have in main you have to allocate as *sigma = (int**) malloc (.... If it is a normal double pointer allocation then it would be ponteiro_duplo = (int**) malloc(...

  • It’s because I’m actually trying to send this double pointer to allocate an array

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.