Oh, yes. Thanks for the advice. I needed some functions to return an int because I thought that I might need it in if statements to test whether memory was allocated and other tasks that might need verification.
About the casting, my compiler issues a warning:
Code:
43 C:\Dev-Cpp\User\LearnC\stash.c [Warning] initialization discards qualifiers from pointer target type
Here's my new bits.
Code:
#define PASS 1
#define FAIL 0
typedef unsigned char byte;
typedef struct StashTag {
int size; /* Size of each block space */
int quantity; /* Number of avaliable spaces */
int items; /* Number of items currently held */
byte* storage; /* Array of bytes */
} Stash;
void init(Stash* s, int block_size) {
s->size = block_size;
s->quantity = 0;
s->items = 0;
s->storage = NULL;
}
void kill(Stash* s) {
if(s->storage != NULL) {
free(s->storage);
}
}
int inflate(Stash* s, int increase) {
byte* temp;
if((temp = (byte*)malloc(s->quantity+increase)) != NULL) {
int old_bytes = s->quantity*s->size;
int j;
for(j = 0; j < old_bytes; ++j) {
temp[j] = s->storage[j];
}
free(s->storage);
s->storage = temp;
s->quantity += increase;
return PASS;
}
else {
return FAIL;
}
}
int store(Stash* s, const void* item) {
if(s->items == s->quantity) {
if(inflate(s, 1) != PASS) {
return FAIL;
}
}
int start_loc = s->items*s->size;
int final_loc = (s->items+1)*s->size;
int j, k;
byte* _item = (byte*)item;
for(j = start_loc, k = 0; j < final_loc; ++j, ++k) {
s->storage[j] = _item[k];
}
++s->items;
return PASS;
}