From 5706a0d893e014acca2b58e747273893a5cf16f0 Mon Sep 17 00:00:00 2001 From: Mikkel Thestrup Date: Thu, 11 Dec 2025 14:38:07 +0100 Subject: Use c99 style of initializing structs --- src/graph.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/graph.c') diff --git a/src/graph.c b/src/graph.c index 8c19a19..2697745 100644 --- a/src/graph.c +++ b/src/graph.c @@ -12,21 +12,22 @@ Graph *graph_new(int n) { if (!graph) return NULL; - graph->vertices = (Vertex *)malloc(sizeof(Vertex) * n); + *graph = (Graph) { + .vertices = (Vertex *)malloc(sizeof(Vertex) * n), + .num_vertices = n, + .num_edges = 0, + }; if (!graph->vertices) { free(graph); return NULL; } - graph->num_edges = 0; - graph->num_vertices = n; - for (int i = 0; i < n; i++) { - graph->vertices[i].id = i; - graph->vertices[i].out_neighbours = vector_new(); - graph->vertices[i].in_neighbours = vector_new(); - - // if allocation for the linked lists failed + graph->vertices[i] = (Vertex) { + .id = i, + .in_neighbours = vector_new(), + .out_neighbours = vector_new(), + }; if (!graph->vertices[i].out_neighbours || !graph->vertices[i].in_neighbours) { graph_delete(graph); -- cgit v1.2.3-70-g09d2