In the textbook my teacher provided us, there is this C code's sample, which when I try to run gives a Segmentation Fault error:
const celula *nulo = NULL;
typedef char informacao;
typedef celula *noh;
typedef celula *arvore;
struct celula {informacao info; noh pai; noh fesq; noh idir;};
...
typedef struct celfloresta celfloresta;
typedef struct celfloresta *floresta;
typedef struct celfloresta *posicfloresta;
struct celfloresta {arvore elem; celfloresta *prox;};
...
void FormarListaNohs(){
floresta p = (floresta)malloc(sizeof(celfloresta));
p->elem->info = '3';
}
...
Why does the line p->elem->info = '3';
give segmentation fault here?
elem
is a pointer. You need to allocate memory for it:
p->elem = malloc(sizeof(arvore));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments