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");
}
}
sorry, had edited the code to be able to ask the question here, and by accident the function call was not edited.
– jaojpaulo
@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.
– Isac
Okay, thanks for the explanation anyway.
– jaojpaulo
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
@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 beponteiro_duplo = (int**) malloc(...
– Isac
It’s because I’m actually trying to send this double pointer to allocate an array
– jaojpaulo