C Saved Program: peaceful-mountain

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


#include <stdlib.h>
#include <stdio.h>

typedef struct IntArray {
    int length;
    int arr[];
} IntArray;

IntArray * createIntArray(int l) {
    if (l > 0) {
        IntArray *obj;
        obj = (IntArray *)malloc(sizeof(IntArray) + sizeof(int) * l);
        obj->length = l;
        obj->arr[l];
        return obj;
    } else {
        printf("Error: Attempting to create array of no or negative size.\n");
        IntArray *obj = NULL;
        return obj;
    }
}

void writeIntArray(IntArray *obj, int pos, int n) {
    if (obj == NULL) {
        printf("Error: Object doesn't exist.\n");
        return;
    }
    if (pos < 0 || pos >= obj->length) {
        printf("Error: Trying to write to nonexistant index.\n");
        return;
    }
    obj->arr[pos] = n;
    return;
}
int readIntArray(IntArray *obj, int pos) {
    if (obj == NULL) {
        printf("Error: Object doesn't exist.\n");
        return -1;
    }
    if (pos < 0 || pos >= obj->length) {
        printf("Error: Trying to read from nonexistant index.\n");
        return -1;
    }
    return obj->arr[pos];
}
void destroyIntArray(IntArray *&obj) {
    if (obj == NULL) {
        printf("Error: Object doesn't exist.\n");
        return;
    }
    free(obj);
    obj = NULL;
    return;
}

typedef struct Bitstream {
    unsigned int atbit;
    unsigned int length;
    unsigned char bitfield[];
} Bitstream;

Bitstream * bs_create(int l) {
    if (l > 0) {
        Bitstream *obj;
        obj = (Bitstream *)malloc(sizeof(Bitstream) + (l * 8));
        obj->atbit = 0;
        obj->length = l * 8;
        for (unsigned int i=0; i<obj->length; i++) obj->bitfield[i]=0;
        return obj;
    } else {
        printf("Error: Attempting to create bitstream of no or negative size.\n");
        Bitstream *obj = NULL;
        return obj;
    }
}
void bs_destroy(Bitstream *&obj) {
    if (obj == NULL) {
        printf("Error: Object doesn't exist.\n");
        return;
    }
    free(obj);
    obj = NULL;
    return;
}
void bs_write(Bitstream *obj, unsigned int bits, unsigned int n) {
    if (obj == NULL) {
        printf("Error: Object doesn't exist.\n");
        return;
    }
    while (bits > 0) {
        if (obj->atbit >= obj->length) {
            printf("Warning: Bitstream has reached maximum capacity.\n");
            break;
        }
        unsigned char b;
        if (n % 2 == 1) {
            b = 128;
        } else {
            b = 0;
        }
        n /= 2;
        bits--;
        unsigned int a = obj->atbit;
        b >>= a - (a / 8) * 8;
        a /= 8;
        obj->bitfield[a] |= b;
        obj->atbit++;
    }
    return;
}

int main(void) {
    
    Bitstream *bs = bs_create(10);
    bs_write(bs, 1, 0);
    bs_write(bs, 1, 1);
    bs_write(bs, 5, 27);
    bs_write(bs, 9, 510);
    bs_write(bs, 3, 3);
    for (unsigned int i=0; i<bs->length / 8; i++) {
        printf("%d ", bs->bitfield[i]);
    }
    bs_destroy(bs);
    
    return EXIT_SUCCESS;
}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs