aboutsummaryrefslogtreecommitdiff
path: root/src/graph.c
diff options
context:
space:
mode:
authorMikkel Thestrup <mithe24@student.sdu.dk>2025-12-11 14:38:07 +0100
committerMikkel Thestrup <mithe24@student.sdu.dk>2025-12-11 14:38:07 +0100
commit5706a0d893e014acca2b58e747273893a5cf16f0 (patch)
tree33416611c66a6b2435581b190317d28fe880245c /src/graph.c
parent22202ef5bb15c55d484cf50d0b39470dbb57cf26 (diff)
downloadcycle-detector-5706a0d893e014acca2b58e747273893a5cf16f0.tar.gz
cycle-detector-5706a0d893e014acca2b58e747273893a5cf16f0.zip
Use c99 style of initializing structs
Diffstat (limited to '')
-rw-r--r--src/graph.c19
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);