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

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    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);
        if(read_vector(v, 0) == NULL)
            printf("%s\n", getlasterrstr());
        printf("%i\n", (int*)(read_vector(v, 1)));
    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. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    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)));
    	printf("%i\n", *((int*)read_vector(v, 1)));
    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. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    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);
    		}
    		for(c = 0;c < index;c++) /* Skip to index */
    		{
    			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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am having a problem with casting
    By blackwyvern in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2002, 02:53 PM
  2. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM
  3. Problem with Casting.
    By pors7 in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2001, 10:50 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Microsoft Visual C++ compiler, cast problem?
    By jonnie75 in forum C Programming
    Replies: 5
    Last Post: 11-10-2001, 08:53 AM