0

I'm trying to insert nodes in a graph using a adjacency list, but it just crashes when i try to insert this line

criaEstacao("Edgware Road", "Verde, Rosa", 200, 0);

on main(). If there is only one insertion it works.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 20
#define TRUE 1
#define FALSE 0


typedef struct no{
    char nome[100];
    char linhas[100];
    int distancia;
    int manutencao;
    struct no *prox;
    struct no *ant;
} No;

No *LinhaAmarela[max];

No *criaEstacao(char n[100], char l[100], int d, int m){
    int i = 0;
    No *novo = (No*)malloc(sizeof(No*));

    strcpy(novo->nome, n);
    strcpy(novo->linhas, l);
    novo->distancia = d;
    novo->manutencao = m;
    novo->prox = NULL;
    novo->ant = NULL;

    while (LinhaAmarela[i] != NULL && i < max){
        i++;
    }

    LinhaAmarela[i] = novo;
}

int main() {
    criaEstacao("Paddington", "Verde, Rosa, Marrom", 200, 0);
    criaEstacao("Edgware Road", "Verde, Rosa", 200, 0);

    getch();
}
Vinicius Coelho
  • 130
  • 2
  • 2
  • 11

1 Answers1

4

Allocate memory for the struct, not the pointer:

No *novo = malloc(sizeof(No));

of better:

No *novo = malloc(sizeof(*novo));   

And always check the return value.

2501
  • 25,460
  • 4
  • 47
  • 87