-
Notifications
You must be signed in to change notification settings - Fork 0
/
htab_init.c
46 lines (38 loc) · 998 Bytes
/
htab_init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// htab_init.c
// Řešení IJC-DU2, příklad 2), 19. 4. 2022
// Autor: Matyáš Strelec, FIT
// Přeloženo: clang 13.1.6
#include <stdio.h>
#include <stdlib.h>
#include "htab.h"
#include "htab_priv.h"
// Table constructor
htab_t *htab_init(size_t n)
{
// Allocate memory for the table
htab_t *table = malloc(sizeof(htab_t));
if (table == NULL)
{
// Malloc error
fprintf(stderr, "Error allocating memory.\n");
return NULL;
}
// Initialize default values
table->size = 0;
table->arr_size = n;
// Allocate memory for the array of items
table->arr_ptr = malloc(n * sizeof(htab_item_t *));
if (table->arr_ptr == NULL)
{
// Malloc error
fprintf(stderr, "Error allocating memory.\n");
free(table);
return NULL;
}
// Initialize all items of array, NULL by default
for (size_t i = 0; i < table->arr_size; i++)
{
table->arr_ptr[i] = NULL;
}
return table;
}