I've been trying to remember old programming concepts like data structures.
My first challenge was about implementing my own array of positive integers:
#include<stdio.h>
#include<stdlib.h>
int* array_initialize(int size) {
return calloc(size, size * sizeof(int));
}
void array_set(int *array, int index, int value) {
array += index;
*array = value;
}
int array_get(int *array, int index) {
array += index;
return *array;
}
int array_size(int *array) {
int size = 0;
while(*array != 0) {
size++;
array++;
}
return size;
}
int main() {
int* array = array_initialize(5);
array_set(array, 0, 2);
array_set(array, 1, 4);
array_set(array, 2, 6);
array_set(array, 3, 8);
array_set(array, 4, 10);
for (int i = 0; i < array_size(array); i++) {
printf("Index #%d -> %d\n", i, array_get(array, i));
}
printf("\nSize: %d\n", array_size(array));
free(array);
return 0;
}
Can I call it an array?
Is it a good starting point? What would you change?
Can I implement my custom array with []
to read/write values to my array?