I've got my "write_vector" function finished, I think it works:
A pointer to the vector I'm writing to is passed in v, the index of the node to write to in index, and a pointer to the data to be copied to another location, which is stored in a node, is passed in data. It returns a pointer to the data in it's stored location.Code:void *write_vector(struct vector *v, int index, void *data) { struct data_node *cur_node; cur_node = v->data; if(v->elem > index) { cur_node = v->data; for(;index;index--) cur_node = cur_node->next; } else { int c; if(v->elem == 0) v->data = cur_node = (struct data_node*)malloc(sizeof(struct data_node)); cur_node->data = calloc(1, v->size); for(c = 0;c < index;c++) { if(c >= v->elem && c < index) { cur_node->next = (struct data_node*)malloc(sizeof(struct data_node)); cur_node->next->data = calloc(1, v->size); } cur_node = cur_node->next; } v->elem = index + 1; } cur_node->data = calloc(1, v->size); return memcpy(cur_node->data, data, v->size); }
And an int main just to debug by playing around with some vectors:
I think it's a problem with my understanding of casting, both line 3 and line 7 of main print the same value. The value changes every time I run the prog, so I'm going to guess that what I'm printing is the location of 5. How do I print the 5? Thanks for reading this far, a lot of code, I knowCode:int i = 5; struct vector *v = init_vector(sizeof(int)); printf("%i\n", write_vector(v, 1, &i)); printf("%i\n", v->elem); if(read_vector(v, 0) == NULL) printf("%s\n", getlasterrstr()); printf("%i\n", (int*)(read_vector(v, 1)));.
My structs being used:
Code:struct data_node{ void* data; struct data_node *next; }; struct vector{ int elem; int size; struct data_node *data; };



LinkBack URL
About LinkBacks
.



(hehe, I made a pun). I was getting a seg fault because it would fail to allocate nodes if the didn't exist, but I fixed it. Here's the completely working version: