This repository was archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
75 lines (50 loc) · 2.69 KB
/
Copy pathexample.c
File metadata and controls
75 lines (50 loc) · 2.69 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#define VECTOR_IMPLEMENTATION
#include <vector.h>
#include <stdio.h>
int main(void) {
vector vec = vector_init();
if (vec == NULL) { /* Make sure the vector was properly intialized. */
printf("Error: Failed to init a vector\n");
return 1;
}
/* Setup some random example variables. */
char* txt = "Some text over here";
char over_allocated_text[256] = "This makes me feel like a rat";
int hex = 0xFF;
char letter = 'A';
vector_push_back(vec, txt, sizeof(txt) + 1); /* Added + 1 for the NULL character. */
vector_push_back(vec, "Labas, pasauli!", sizeof("Labas, pasauli!") + 1);
int i;
for (i = 0; i < vector_size(vec); i++) { /* We get the element count of the array (array's size/length) via vector_size(vec). With that we can iterate through the vector. */
printf("At index '%i', we have value \"%s\"\n", i, vector_get(vec, i)); /* Since we know all of the elements in the array are strings, we can print them via the string format. */
}
txt = "CHANGED TEXT OVER HERE";
printf("Even though variable 'txt' now equals \"%s\", 'vec' variable's index 0 doesn't get overwritten as its value is still \"%s\"\n", txt, vector_get(vec, 0));
/*
Delete the first element as we don't need it anymore.
Since the first element is erased, every other elements' index decrements by one (meaning the second value's index is now 0, not 1).
*/
vector_erase(vec, 0);
printf("Current size of 'vec': %i\n", vector_size(vec));
/*
Since we know we only need to copy the *actual* value of the string (and not the entire item),
we can just tell the function to only copy the bytes that necessary (in this case, the length of the text plus NULL character).
*/
vector_set(vec, 0, over_allocated_text, strlen(over_allocated_text) + 1);
printf("Index 0 is now \"%s\"\n", vector_get(vec, 0));
/* We can also insert any other type other than char since this is a void*. */
vector_push_back(vec, &hex, sizeof(int));
vector_push_back(vec, &letter, sizeof(letter));
printf("Current size of 'vec': %i\n", vector_size(vec));
vector_clear(vec); /* Removes every value from the vector. */
printf("Current size of 'vec': %i\n", vector_size(vec));
unsigned char k;
for (k = ' '; k < 0xFF; k++) /* Copy every ASCII leter (including those in the extended table) from space to the last character code 0xFF. */
vector_push_back(vec, &k, sizeof(k));
char* ascii_letters = vector_str(vec);
printf("The string version of the entire vector: \"%s\" (%i element count)\n", ascii_letters, vector_size(vec));
vector_pop_back(vec); /* Pop the back element. */
printf("We now only have %i elements left in the vector\n", vector_size(vec));
vector_free(vec); /* Free the variable since we don't wanna use it anymore.*/
return 0;
}