diff options
| author | Mikkel Thestrup <mithe24@student.sdu.dk> | 2025-12-11 14:38:07 +0100 |
|---|---|---|
| committer | Mikkel Thestrup <mithe24@student.sdu.dk> | 2025-12-11 14:38:07 +0100 |
| commit | 5706a0d893e014acca2b58e747273893a5cf16f0 (patch) | |
| tree | 33416611c66a6b2435581b190317d28fe880245c /src/graph.c | |
| parent | 22202ef5bb15c55d484cf50d0b39470dbb57cf26 (diff) | |
| download | cycle-detector-5706a0d893e014acca2b58e747273893a5cf16f0.tar.gz cycle-detector-5706a0d893e014acca2b58e747273893a5cf16f0.zip | |
Use c99 style of initializing structs
Diffstat (limited to '')
| -rw-r--r-- | src/graph.c | 19 |
1 files changed, 10 insertions, 9 deletions
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); |