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