Created
July 19, 2020 23:10
-
-
Save levicole/2915c0f9e2c69d23c99e68a70e6595b7 to your computer and use it in GitHub Desktop.
dynamic array thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
// I have no idea if this is a good idea, or a bad idea? | |
typedef enum { | |
K_STRING, | |
K_INT, | |
K_COMPLEX | |
} kind; | |
typedef struct complex_type { | |
int number; | |
char *message; | |
} complex_type; | |
typedef struct element { | |
kind type; | |
size_t width; | |
void *data; | |
} element; | |
typedef struct d_array { | |
int max_size; | |
int end; | |
element **elements; | |
} d_array; | |
void die(char *message) | |
{ | |
printf("Error: %s", message); | |
exit(1); | |
} | |
d_array *create_d_array(int initial_size) | |
{ | |
d_array *array; | |
if((array = malloc(sizeof(d_array))) == NULL) die("Couldn't allocate d_array"); | |
array->max_size = initial_size; | |
array->end = 0; | |
array->elements = calloc(initial_size, sizeof(element *)); | |
return array; | |
} | |
int array_push(d_array *array, kind type, size_t width, void *thing) | |
{ | |
// add some logic to expand the size of the array, but for now, die if adding this element will be too big | |
if (array->end == (array->max_size - 1)) return 0; | |
element *el; | |
if((el = malloc(sizeof(element))) == NULL) die("Couldn't allocate el"); | |
el->type = type; | |
el->width = width; | |
el->data = malloc(el->width); | |
memcpy(el->data, thing, el->width); | |
array->elements[array->end] = el; | |
array->end++; | |
return 1; | |
} | |
void element_pp(element *el) | |
{ | |
complex_type *complex; | |
switch (el->type) | |
{ | |
case K_INT: | |
printf("The value is: %d\r\n", *(int *)el->data); | |
break; | |
case K_STRING: | |
printf("The value is: %s\r\n", (char *)el->data); | |
break; | |
case K_COMPLEX: | |
complex = (complex_type *)el->data; | |
printf("The number is %d and the message is %s\r\n", complex->number, complex->message); | |
break; | |
default: | |
printf("I don't know what this is\r\n"); | |
break; | |
} | |
} | |
int main(void) { | |
d_array *array; | |
printf("sizeof(size_t) = %lu\r\n", sizeof(size_t)); | |
array = create_d_array(10); | |
int fortytwo = 42; | |
char *message = "42 is the answer..."; | |
complex_type *fun; | |
fun = malloc(sizeof(complex_type)); | |
fun->message = "This is fun"; | |
fun->number = 10; | |
array_push(array, K_INT, sizeof(int), &fortytwo); | |
array_push(array, K_STRING, strlen(message), message); | |
array_push(array, K_COMPLEX, sizeof(complex_type), fun); | |
element_pp(array->elements[0]); | |
element_pp(array->elements[1]); | |
element_pp(array->elements[2]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment