Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define STEP 256
- void checkAlloc(void *p) {
- if (p == NULL) {
- perror(NULL);
- exit(-1);
- }
- }
- typedef struct {
- int len, mem, *at;
- } DynArr;
- void addToArr(DynArr *arr, int x) {
- if (arr->len == arr->mem) {
- arr->mem += STEP;
- arr->at = realloc(arr->at, sizeof(int) * arr->mem);
- }
- arr->at[arr->len++] = x;
- }
- void revArr(DynArr *arr) {
- int len = arr->len;
- for (int i = 0; i < len / 2; i++) {
- int aux = arr->at[i];
- arr->at[i] = arr->at[len - i - 1];
- arr->at[len - i - 1] = aux;
- }
- }
- void printArr(const DynArr *const arr) {
- for (int i = 0; i < arr->len; i++) {
- printf("%d ", arr->at[i]);
- }
- puts("");
- }
- int main(void) {
- DynArr arr = {0, 0, NULL};
- for (int x; scanf("%d", &x) && x;) {
- addToArr(&arr, x);
- }
- revArr(&arr);
- printArr(&arr);
- free(arr.at);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement