#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    [SOLVED]Void * .. HELP!!!!


    Hello,
    I'm coding a sorted list "library" that is included in any .c file to create a list that maintains the order of any type of object you pass to it.

    In order to accomplish that I used a singly linked list and each node holds a void * data in it.

    Now I'm facing a problem , I can't get rid of a warning saying "error: invalid use of void expression".

    Here's the code:

    Code:
    int SLInsert(SortedListPtr list, void *newObj){
    
    	void *tempObj;
    	
    	nodePtr newNode;
    	newNode = (nodePtr)malloc(sizeof(struct node));
    	if(newNode==NULL)
    		return 0;
        
            tempObj = malloc(sizeof(newObj));
            if(tempObj==NULL)
    		return 0;
            *(void*)tempObj = *newObj; //this is the line that isn't working
    	newNode->data = tempObj;
    	newNode->nPointers = 0;
    
    ....
    
    }
    I'm basically trying to say: whatever newObj is pointing to assign it to what tempObj is pointing to.

    What I'm I doing wrong? how can I accomplish this? I basically need to store any data in the void * and make it unique.

    Thanks in advance!!
    Last edited by Cataladu; October 7th, 2012 at 02:25 AM. Reason: [SOLVED]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Why are you allocating memory for your pointer? Your pointer is going to point to the object address passed to it right? Just set the pointer to the value passed in via the function.

    Code:
    tempObj = newObj;
    Also. This makes no sense

    Code:
    *(void*)of_something
    Your asking for the value of a void which is a no-no.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by G4143
    Why are you allocating memory for your pointer? Your pointer is going to point to the object address passed to it right? Just set the pointer to the value passed in via the function.
    The problem is that if I do that, then when I call it in my main with a for loop like this:

    srand(time(NULL));
    iterator = SLCreateIterator(list1);

    for(i=0;i<10;i++){
    r = rand() % 10;

    printf(" n:%i ",&r);
    was_inserted = SLInsert(list1,random);

    if(was_inserted==0){
    printf("ERROR: OBJECT COULDN'T BE ADDED\n");
    exit(0);
    }
    else{
    sixthIter = SLCreateIterator(list1);
    printf("List Head->");
    while((n = SLNextItem(sixthIter))!=NULL){
    printf("%i->",(int)*n);
    }
    printf("List Tail\n");
    }


    }

    I get this output (list has already a 5):
    r: 1
    OBJECT TO BE ADDED: 1
    comparator returned: -1
    List Head->5->1->List Tail
    r: 7
    OBJECT TO BE ADDED: 7
    List Head->7->5->7->List Tail
    r: 9
    OBJECT TO BE ADDED: 9
    List Head->9->9->5->9->List Tail
    r: 0
    OBJECT TO BE ADDED: 0
    List Head->0->0->0->5->0->List Tail
    r: 9
    OBJECT TO BE ADDED: 9
    List Head->9->9->9->9->5->9->List Tail
    r: 8
    OBJECT TO BE ADDED: 8
    List Head->8->8->8->8->8->5->8->List Tail
    r: 5
    OBJECT TO BE ADDED: 5
    List Head->5->5->5->5->5->5->5->5->List Tail
    r: 9
    OBJECT TO BE ADDED: 9
    List Head->9->9->9->9->9->9->9->5->9->List Tail
    r: 4
    OBJECT TO BE ADDED: 4
    List Head->4->4->4->4->4->4->4->4->5->4->List Tail
    r: 0
    OBJECT TO BE ADDED: 0
    List Head->0->0->0->0->0->0->0->0->0->5->0->List Tail


    which is certainly not what I want, and what I guess is that everytime the random pointer changes its value so does node->data , because it's pointing to it,
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by Cataladu
    The problem is that if I do that, then when I call it in my main with a for loop like this:

    srand(time(NULL));
    iterator = SLCreateIterator(list1);

    for(i=0;i<10;i++){
    r = rand() % 10;

    printf(" n:%i ",&r);
    was_inserted = SLInsert(list1,random);

    if(was_inserted==0){
    printf("ERROR: OBJECT COULDN'T BE ADDED\n");
    exit(0);
    }
    else{
    sixthIter = SLCreateIterator(list1);
    printf("List Head->");
    while((n = SLNextItem(sixthIter))!=NULL){
    printf("%i->",(int)*n);
    }
    printf("List Tail\n");
    }


    }

    I get this output (list has already a 5):
    r: 1
    OBJECT TO BE ADDED: 1
    comparator returned: -1
    List Head->5->1->List Tail
    r: 7
    OBJECT TO BE ADDED: 7
    List Head->7->5->7->List Tail
    r: 9
    OBJECT TO BE ADDED: 9
    List Head->9->9->5->9->List Tail
    r: 0
    OBJECT TO BE ADDED: 0
    List Head->0->0->0->5->0->List Tail
    r: 9
    OBJECT TO BE ADDED: 9
    List Head->9->9->9->9->5->9->List Tail
    r: 8
    OBJECT TO BE ADDED: 8
    List Head->8->8->8->8->8->5->8->List Tail
    r: 5
    OBJECT TO BE ADDED: 5
    List Head->5->5->5->5->5->5->5->5->List Tail
    r: 9
    OBJECT TO BE ADDED: 9
    List Head->9->9->9->9->9->9->9->5->9->List Tail
    r: 4
    OBJECT TO BE ADDED: 4
    List Head->4->4->4->4->4->4->4->4->5->4->List Tail
    r: 0
    OBJECT TO BE ADDED: 0
    List Head->0->0->0->0->0->0->0->0->0->5->0->List Tail


    which is certainly not what I want, and what I guess is that everytime the random pointer changes its value so does node->data , because it's pointing to it,
    And I'm sure that makes sense to you but to me its gibberish.

    Understanding what a void and a void* is, is sometimes confusing to the new C/c++'er but its essential. I would take the time to 'backtrack' to basic pointer information.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by G4143
    And I'm sure that makes sense to you but to me its gibberish.

    Understanding what a void and a void* is, is sometimes confusing to the new C/c++'er but its essential. I would take the time to 'backtrack' to basic pointer information.
    Yeah, I'm sorry I understand what you say, let me explain a little bit better and see if you can catch the problem.

    I have 3 files. sorted-list.h , sorted-list.c and a main.c . My sorted-list.h defines my data structure as following:

    Code:
    struct node
    {
    	void * data;
    	int nPointers;
    	struct node * next;
    };
    typedef struct node* nodePtr;
    
    struct SortedList
    {
    	nodePtr head;
    };
    typedef struct SortedList* SortedListPtr;
    
    
    struct SortedListIterator
    {
    	nodePtr current;
    };
    typedef struct SortedListIterator* SortedListIteratorPtr;
    
    
    typedef int (*CompareFuncT)(void *, void *);
    
    
    SortedListPtr SLCreate(CompareFuncT cf);
    
    void SLDestroy(SortedListPtr list);
    
    int SLInsert(SortedListPtr list, void *newObj);
    
    int SLRemove(SortedListPtr list, void *newObj);
    
    SortedListIteratorPtr SLCreateIterator(SortedListPtr list);
    
    void SLDestroyIterator(SortedListIteratorPtr iter);
    
    void *SLNextItem(SortedListIteratorPtr iter);
    And I'm implementing all functions in my sorted-list.c file. the one function that i'm most concerned about is this one:

    Code:
    int SLInsert(SortedListPtr list, void *newObj){
    
    	void *tempObj;
    	int n,*tn,*tn2;
    	nodePtr newNode, temp, temp2;
    	newNode = (nodePtr)malloc(sizeof(struct node));
    	if(newNode==NULL)
    		return 0;
           printf("\nOBJECT TO BE ADDED: %i \n",*((int*)newObj));
          
    	newNode->data = newObj;
    	newNode->nPointers = 0;
    
    
    	/*case list is empty*/
    	if(list->head==NULL){
    
    		list->head=newNode;
    		list->head->next = NULL;
    		if(iteratorCreated==0){
    		  list->head->nPointers++;
    		}
    		else{
    		  list->head->nPointers=2;
    		}
    	}
    	else{
          /* printf("There's one ore more elements in the list\n");*/
    		temp = list->head;
    		if(temp->next==NULL){ /*If there's only one element*/
    		    /*printf("there's only one element\n");*/
    			n = comparator(newObj,list->head->data);
    			printf("comparator returned: %i\n",n);
    			if(n==1||n==0){ /*case newObj is >= than temp->data*/
    
    				newNode->next=temp;
    				/*printf("node->next linked, data linked next node: %i\n",(int)newNode->next->data);*/
    				list->head= newNode;
    				/*printf("new node placed, data linked node: %i\n",(int)list->head->data);*/
    				list->head->nPointers++;
    				/*printf("new node nPointers: %i\n",list->head->nPointers);*/
    
    			}
    			else{
    				temp->next= newNode;
    				newNode->next= NULL;
    				newNode->nPointers++;
    			}
    
    		}
    		else{ /*there are more than one element in the list*/
    			/*Check if node is going to be the new head, newObj >= list.data*/
    			if(comparator(newObj,list->head->data)==1||comparator(newObj,list->head->data)==0){
    				temp = list->head;
    				list->head=newNode;
    				newNode->next = temp;
    				newNode->nPointers++;
    
    			}
    			else{
                    /*node to add is not going to be new head*/
    
    
                    while(temp!=NULL){
                        tn = temp->data;
                        printf("\ntemp->data:: %i \n",*(tn));
                         if(temp->next==NULL){
                             printf("\notemp->next==null\n");
                              temp->next = newNode;
                              newNode->next = NULL;
                              newNode->nPointers++;
                              break;
                         }
                         /*case : obj< temp and obj > temp->next OR obj = temp*/
                         else if((comparator(newObj,temp->data))==0||
                                ((comparator(newObj,temp->data))==-1&&(comparator(newObj,temp->next->data))==1)){
                             tn2 = temp->next->data;
                             printf("\nHERE. temp->data: %i AND temp->next->data: %i\n",*(tn),*(tn2));
                             temp2= temp->next;
                             temp->next = newNode;
                             newNode->next = temp2;
                             newNode->nPointers++;
                             break;
                         }
                        printf("\nNote here, next number\n");
                        temp=temp->next;
                    }
    			}
    
    		}
    
    
    	}
    
    	return 1;
    }
    That function basically inserts the object to the right position keeping the correct order in the list.

    Now, my main has this cases:


    Code:
    #include <stdio.h>
    #include <time.h>
    #include "sorted-list.h"
    
    
    int compare(int* ,int* );
    
    
    int main(int argc, char ** argv){
    
    	/*First case: basics. create list, add number, create iterator and use it*/
    
    
    	SortedListPtr list1 ;
    	SortedListIteratorPtr firstIter, secondIter, thirdIter, fourthIter;
    	SortedListIteratorPtr fifthIter, sixthIter, seventhIter, eighthIter;
    	int was_inserted ;
    	int was_removed ;
    	int *n;
    	int *Number, *random;
    	int n1,n2,n3,n4,n5,n6,n7,n8, r , i;
    
    	Number= (int*)malloc(sizeof(int));
    
    
        /*First case: create a list, insert and element, create an iterator and use it*/
        printf("FIRST CASE\n");
    
        list1 =  SLCreate((CompareFuncT)compare); /*creates the list*/
        n1=5;
        Number = &n1;
        was_inserted = SLInsert(list1,Number); /*inserts the first element*/
    
        if(was_inserted==1){
    
          firstIter = SLCreateIterator(list1);
    
          printf("List Head->");
          while((n = SLNextItem(firstIter))!=NULL){
    
    			printf("%i->",(int)*n);
          }
          printf("List Tail\n");
        }
        else{
          printf("ERROR: OBJECT COULDN'T BE ADDED\n");
          exit(0);
        }
    
        /*second case: adding element to a list containing only one element. This new element is
        >= than the element currently on the list*/
    
        printf("-----------------------------------\nSECOND CASE\n");
        n2=6;
        Number = &n2;
        was_inserted = SLInsert(list1,Number); /*inserts the another element >= than current */
    
        if(was_inserted==1){
    
          secondIter = SLCreateIterator(list1);
          printf("List Head->");
          while((n = SLNextItem(secondIter))!=NULL){
    			printf("%i->",(int)*n);
          }
          printf("List Tail\n");
    
        }
        else{
          printf("ERROR: OBJECT COULDN'T BE ADDED\n");
          exit(0);
        }
    
    
    
    
    	/*case 3: remove element (number 6 which is snumber*/
    	printf("-----------------------------------\nTHIRD CASE\n");
    	was_removed = SLRemove(list1,Number);
    	if(was_removed==1){
    
          thirdIter = SLCreateIterator(list1);
          printf("List Head->");
          while((n = SLNextItem(thirdIter))!=NULL){
    			printf("%i->",(int)*n);
          }
          printf("List Tail\n");
    
        }
        else{
          printf("ERROR: OBJECT COULDN'T BE REMOVE ( NOT FOUND? )\n");
          exit(0);
        }
    
        /*case 4: having only one element on the list, now add an item < than current item in the list*/
        printf("-----------------------------------\nFOURTH CASE\n");
    
        n3=4;
        Number = &n3;
        was_inserted = SLInsert(list1,Number); /*inserts the another element >= than current */
    
        if(was_inserted==1){
    
          fourthIter = SLCreateIterator(list1);
          printf("List Head->");
          while((n = SLNextItem(fourthIter))!=NULL){
    			printf("%i->",(int)*n);
          }
          printf("List Tail\n");
    
        }
        else{
          printf("ERROR: OBJECT COULDN'T BE ADDED\n");
          exit(0);
        }
    
        /*case 5: having more than one element add one to tail (smaller than all)*/
    
        printf("-----------------------------------\nFOURTH CASE\n");
        n4=1;
        Number = &n4;
        was_inserted = SLInsert(list1,Number); /*inserts the another element >= than current */
    
        if(was_inserted==1){
    
          fourthIter = SLCreateIterator(list1);
          printf("List Head->");
          while((n = SLNextItem(fourthIter))!=NULL){
    			printf("%i->",(int)*n);
          }
          printf("List Tail\n");
    
        }
        else{
          printf("ERROR: OBJECT COULDN'T BE ADDED\n");
          exit(0);
        }
    
    
    
        /*case 6: having more than one element add one to head( bigger than all)*/
        printf("-----------------------------------\nFIFTH CASE\n");
        n5=10;
        Number = &n5;
        was_inserted = SLInsert(list1,Number); /*inserts the another element >= than current */
    
        if(was_inserted==1){
    
          fifthIter = SLCreateIterator(list1);
          printf("List Head->");
          while((n = SLNextItem(fifthIter))!=NULL){
    			printf("%i->",(int)*n);
          }
          printf("List Tail\n");
    
        }
        else{
          printf("ERROR: OBJECT COULDN'T BE ADDED\n");
          exit(0);
        }
    
    
        /*case 7: having more than one element add one at a random position ( middle )*/
        printf("-----------------------------------\nSIXTH CASE\n");
        n6=2;
        Number = &n6;
        was_inserted = SLInsert(list1,Number); /*inserts the another element >= than current */
    
        if(was_inserted==1){
    
          sixthIter = SLCreateIterator(list1);
          printf("List Head->");
          while((n = SLNextItem(sixthIter))!=NULL){
    			printf("%i->",(int)*n);
          }
          printf("List Tail\n");
    
        }
        else{
          printf("ERROR: OBJECT COULDN'T BE ADDED\n");
          exit(0);
        }
    
        /*case 8: add at least 10 randomly generated numbers */
        printf("-----------------------------------\nEIGHT CASE\n");
        srand(time(NULL));
        sixthIter = SLCreateIterator(list1);
        printf("List Head->");
        while((n = SLNextItem(sixthIter))!=NULL){
            printf("%i->",(int)*n);
        }
             printf("List Tail\n");
        for(i=0;i<10;i++){
            r = rand() % 10;
            printf("r: %i ",r);
            random = (int*)malloc(sizeof(int));
            *random = r;
            printf(" n:%i ",*random);
            was_inserted = SLInsert(list1,random); /*inserts the another element >= than current */
    
            if(was_inserted==0){
              printf("ERROR: OBJECT COULDN'T BE ADDED\n");
              exit(0);
            }
            else{
             sixthIter = SLCreateIterator(list1);
             printf("List Head->");
             while((n = SLNextItem(sixthIter))!=NULL){
    			printf("%i->",(int)*n);
             }
             printf("List Tail\n");
            }
    
        }
    
    
    
        /*case 9: add at least 100 numbers */
    
        /*case 10: add at least 1000 items */
    
        /*case 11: add at least 1000000 items */
    
        /*case 12: remove all items */
    
        /*case 13: destroy list and iterator */
    
        return 0;
    }
    
    
    int compare(int * first,int * second){
        /*printf("first: %i ",*first);
        printf("second:%i = ",*second);*/
    
    	if(*first<*second){
    	   /* printf(" smaller\n");*/
    		return -1;
    	}
    	else if(*first>*second){
    	    /*printf(" greater\n");*/
    		return 1;
    	}
    	else{
    	   /* printf(" equal\n");*/
    		return 0;
    	}
    
    }
    and this is the output I get when i run it:

    Code:
    ./Sorted\ List
    FIRST CASE
    
    OBJECT TO BE ADDED: 5 
    List Head->5->List Tail
    -----------------------------------
    SECOND CASE
    
    OBJECT TO BE ADDED: 6 
    comparator returned: 1
    List Head->6->5->List Tail
    -----------------------------------
    THIRD CASE
    List Head->5->List Tail
    -----------------------------------
    FOURTH CASE
    
    OBJECT TO BE ADDED: 4 
    comparator returned: -1
    List Head->5->4->List Tail
    -----------------------------------
    FIFTH CASE
    
    OBJECT TO BE ADDED: 1 
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 4 
    
    otemp->next==null
    List Head->5->4->1->List Tail
    -----------------------------------
    FIFTH CASE
    
    OBJECT TO BE ADDED: 10 
    List Head->10->5->4->1->List Tail
    -----------------------------------
    SIXTH CASE
    
    OBJECT TO BE ADDED: 2 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 4 
    
    HERE. temp->data: 4 AND temp->next->data: 1
    List Head->10->5->4->2->1->List Tail
    -----------------------------------
    EIGHT CASE
    List Head->10->5->4->2->1->List Tail
    r: 5  n:5 
    OBJECT TO BE ADDED: 5 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 5 
    
    HERE. temp->data: 5 AND temp->next->data: 4
    List Head->10->5->5->4->2->1->List Tail
    r: 5  n:5 
    OBJECT TO BE ADDED: 5 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 5 
    
    HERE. temp->data: 5 AND temp->next->data: 5
    List Head->10->5->5->5->4->2->1->List Tail
    r: 2  n:2 
    OBJECT TO BE ADDED: 2 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 4 
    
    Note here, next number
    
    temp->data:: 2 
    
    HERE. temp->data: 2 AND temp->next->data: 1
    List Head->10->5->5->5->4->2->2->1->List Tail
    r: 7  n:7 
    OBJECT TO BE ADDED: 7 
    
    temp->data:: 10 
    
    HERE. temp->data: 10 AND temp->next->data: 5
    List Head->10->7->5->5->5->4->2->2->1->List Tail
    r: 2  n:2 
    OBJECT TO BE ADDED: 2 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 7 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 4 
    
    Note here, next number
    
    temp->data:: 2 
    
    HERE. temp->data: 2 AND temp->next->data: 2
    List Head->10->7->5->5->5->4->2->2->2->1->List Tail
    r: 5  n:5 
    OBJECT TO BE ADDED: 5 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 7 
    
    Note here, next number
    
    temp->data:: 5 
    
    HERE. temp->data: 5 AND temp->next->data: 5
    List Head->10->7->5->5->5->5->4->2->2->2->1->List Tail
    r: 9  n:9 
    OBJECT TO BE ADDED: 9 
    
    temp->data:: 10 
    
    HERE. temp->data: 10 AND temp->next->data: 7
    List Head->10->9->7->5->5->5->5->4->2->2->2->1->List Tail
    r: 4  n:4 
    OBJECT TO BE ADDED: 4 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 9 
    
    Note here, next number
    
    temp->data:: 7 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 5 
    
    Note here, next number
    
    temp->data:: 4 
    
    HERE. temp->data: 4 AND temp->next->data: 2
    List Head->10->9->7->5->5->5->5->4->4->2->2->2->1->List Tail
    r: 8  n:8 
    OBJECT TO BE ADDED: 8 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 9 
    
    HERE. temp->data: 9 AND temp->next->data: 7
    List Head->10->9->8->7->5->5->5->5->4->4->2->2->2->1->List Tail
    r: 5  n:5 
    OBJECT TO BE ADDED: 5 
    
    temp->data:: 10 
    
    Note here, next number
    
    temp->data:: 9 
    
    Note here, next number
    
    temp->data:: 8 
    
    Note here, next number
    
    temp->data:: 7 
    
    Note here, next number
    
    temp->data:: 5 
    
    HERE. temp->data: 5 AND temp->next->data: 5
    List Head->10->9->8->7->5->5->5->5->5->4->4->2->2->2->1->List Tail

    Notice how all my cases 1 through 7 work perfectly. But when I get to case 8, everything brakes. And I was telling you before that I believe it's because for some reason the data saved on each node is pointing to a number that keeps changing and thus when I print the number it prints the current randomly generated number at all newly created nodes.

    I hope you can understand this. It's kind of hard to explain a whole program in just a single post
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    [solved]


    I figured it out.

    Code:
    int SLInsert(SortedListPtr list, void *newObj){
    
        void * tempObj;
    	int n,*tn,*tn2;
    	nodePtr newNode, temp, temp2;
    	newNode = (nodePtr)malloc(sizeof(struct node));
    	if(newNode==NULL)
    		return 0;
    
        tempObj = (void*)malloc(sizeof(newObj));
        (void) memcpy(tempObj, newObj, sizeof(newObj));
    	newNode->data = tempObj;
    	newNode->nPointers = 0;
    that line guaranteed my data was always going to be unique, by copying the value to another memory allocated pointer.

IMN logo majestic logo threadwatch logo seochat tools logo