Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Dynamic array
//dynamic array #include <stdio.h> typedef struct { int *array; size_t used; size_t size; } Array; void initArray(Array *a, size_t initialSize) { a->array = (int *)malloc(initialSize * sizeof(int)); a->used = 0; a->size = initialSize; } void insertArray(Array *a, int element) { if (a->used == a->size) { a->size *= 2; a->array = (int *)realloc(a->array, a->size * sizeof(int)); } a->array[a->used++] = element; } void freeArray(Array *a) { free(a->array); a->array = NULL; a->used = a->size = 0; } int main(void) { Array a; int i; initArray(&a, 5); // initially 5 elements for (i = 0; i < 100; i++) insertArray(&a, i); // automatically resizes as necessary printf("%d\n", a.array[9]); // print 10th element printf("%d\n", a.used); // print number of elements freeArray(&a); return 0; }
run
|
edit
|
history
|
help
0
hello world
18BCE2182 ASSESS_1 Q1-5
EUCLIDES BIEN
Official Hello World
stringify
Card shuffling and dealing program using structures
bitwise operations
pruebapi2
prueba 3
lab 11 v0.7