C malloc segmentation fault struct

Benuck

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?

Saucy Goat

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Malloc to struct; segmentation fault

From Dev

Segmentation fault with malloc on char* in struct

From Dev

Segmentation fault in C with malloc

From Dev

C struct Segmentation fault

From Dev

C - malloc and structures, segmentation fault

From Dev

malloc causes segmentation fault in C

From Dev

Segmentation Fault C Arrays and Malloc

From Dev

C struct array segmentation fault

From Dev

Exited segmentation fault Struct in C

From Dev

Why does this C struct need a malloc to create it but shows segmentation fault otherwise?

From Dev

C - Segmentation Fault (core dumbed) with malloc()

From Dev

Get segmentation fault when manipulating with malloc in c

From Dev

C segmentation fault using malloc'ed strings

From Dev

Malloc causes segmentation fault when allocating a struct for a linked-list

From Dev

Segmentation fault in malloc replication

From Dev

Segmentation Fault on malloc()

From Dev

malloc and realloc segmentation fault

From Dev

segmentation fault by malloc

From Dev

custom malloc, segmentation fault

From Dev

Segmentation fault in malloc()

From Dev

Returning struct in C function causes segmentation fault

From Dev

Segmentation fault after allocating memory for a struct in C

From Dev

c++ Segmentation Fault using struct pointer

From Dev

C - Segmentation Fault Assigning value to Array of Struct

From Dev

Segmentation Fault when using qsort on struct in C

From Dev

Segmentation Fault in typedef struct

From Dev

Struct causing segmentation fault

From Dev

Segmentation fault: Struct Pointer

From Dev

Segmentation fault on a null struct?