# Thread: Cast void* to int* problem. I think my understanding of casting is flawed.

1. ## Cast void* to int* problem. I think my understanding of casting is flawed.

I've got my "write_vector" function finished, I think it works:
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);
}```
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.

And an int main just to debug by playing around with some vectors:
Code:
```    int i = 5;
struct vector *v = init_vector(sizeof(int));
printf("%i\n", write_vector(v, 1, &i));
printf("%i\n", v->elem);
printf("%s\n", getlasterrstr());
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 know .

My structs being used:
Code:
```struct data_node{
void* data;
struct data_node *next;
};

struct vector{
int elem;
int size;
struct data_node *data;
};```

2. Okay, apparently I don't fully understand pointers when used in casting. Here's what main should look like: (minus some other debigging stuff)
Code:
```int main(int argc, char **argv)
{
int i = 5;
struct vector *v = init_vector(sizeof(int));
printf("%i\n", *((int*)write_vector(v, 1, &i)));
write_vector writes the 5 into memory(and also returns a pointer to it) and the read_vector returns a pointer to it.

Is there a standard vector lib for C?

3. It is clearer to assign to temporary pointer.
Code:
```  int *p = write_vector(v,1,&i);
printf("%i\n", *p);
printf("%i\n",   *(int*)write_vector(v,1,&i)  );   // cast void pointer to int pointer and
// deference```

4. Yeah, that looks a lot cleaner. Thanks for the pointer (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:
Code:
```void *write_vector(struct vector *v, int index, void *data)
{
struct data_node *cur_node;
cur_node = v->data; /* Set the current node to the root node */
if(v->elem > index) /* Check to see if the index is within the bounds of the vector, if it is, do this the easy way */
{
cur_node = v->data;
for(;index;index--)
cur_node = cur_node->next;
}
else /* If it's not, do it the hard way */
{
int c;
if(v->elem == 0) /* Check to see the root node exists */
{
v->data = cur_node = (struct data_node*)malloc(sizeof(struct data_node));
cur_node->data = calloc(1, v->size);
}
{
if((c + 1) >= v->elem && (c + 1) <= index) /* Does the next node exist, if not... */
{
cur_node->next = (struct data_node*)malloc(sizeof(struct data_node)); /* Allocate memory for it */
cur_node->next->data = calloc(1, v->size); /* Allocate memory for it's data */
}
cur_node = cur_node->next;
}
if(index >= v->elem)
v->elem = index + 1;
}
return memcpy(cur_node->data, data, v->size);
}```