Guest User

Untitled

a guest
Apr 19th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. int* my_array = malloc(10 * sizeof(int));
  2. int n_used_elements = 0; // Need to keep track of the used elements and the size
  3. int my_array_size = 10; // reserved size
  4.  
  5. int myArray[10];
  6.  
  7. int* myArray = malloc(10, sizeof(int));
  8.  
  9. typedef struct {
  10. int *array;
  11. size_t used;
  12. size_t size;
  13. } Array;
  14.  
  15. void insertArray(Array *a, int element) {
  16. if (a->used == a->size) {
  17. a->size *= 2; // double the size when exceeding the size of the array
  18. a->array = (int *)realloc(a->array, a->size * sizeof(int));
  19. }
  20. a->array[a->used++] = element;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment