Thread: Help free structs and pointer.

  1. #31
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This I've got to see.

  2. #32
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Code:
    LIST_ELEM *add(LIST_ELEM *pHead,char type[],char name[], char title[], int year, double price, int size){
    
    	LIST_ELEM *new_elem = malloc(sizeof(LIST_ELEM));
    
    	strcpy(new_elem->type, type);
    	strcpy(new_elem->name,name);
    	strcpy(new_elem->title, title);
    
    	new_elem->year = year;
    	new_elem->price = price;
    	new_elem->size = size;
    	new_elem->pNext = pHead;
    
    	return new_elem;
    
    }

  3. #33
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    lol i think you helped me out on this the other day =)

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Oh, you're returning the new head instead of changing the parameter.

    Well ... why not do that here? Instead of returning void, return a LIST_ELEM* that's the new head of the list.

  5. #35
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    so I return pHead but that didn't solve my whole removal issue. Going back to the whole *ptr and **ptr deal, would I still have to do that?

  6. #36
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    HAH HAH finally getting somewhere. So I've got the case were the list is 1 element, and the element is the first element to work. But I can't get the last case to work, where the element is somewhere in the middle of the list. Can you spot where I'm going wrong? i get a seg fault. when i try this.


    Code:
     else if( i == loc){
    		LIST_ELEM *ptrB4 = nodeB4(pHead,loc);
    		tmpPtr = ptr;
    		LIST_ELEM *ptrAfter = (tmpPtr->pNext);
    		ptrB4->pNext = ptrAfter;
    		free(tmpPtr);
    		break;
    		}

  7. #37
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Not really; you've got two choices:
    Option 1: return void, change the parameter
    Code:
    void removePtr(LIST_ELEM **pHead, int loc) {
        LIST_ELEM *prevPtr, *nextPtr, *tempPtr;
        int size = sizeOf(*pHead);
    
        if (loc == 0) {
            if (size == 0) return; //already empty
            prevPtr = (*pHead)->next;
            free(*pHead);
            *pHead = prevPtr;
            return;
        }
    
        if (size < loc) return; //can't delete element that doesn't exist
    
        //if we're still here then walk to the element and delete it
        tempPtr = *pHead;
        for (int i = 0; i < loc; i++) {
            tempPtr = tempPtr->next;
        }
        prevPtr = nodeB4(*pHead, loc); //if we didn't have the function, we'd walk prevPtr with tempPtr
        nextPtr = tempPtr->next;
        prevPtr->next = nextPtr;
        free(tempPtr);
        return;
    }
    Option 2, return the head of the list (consistent with your add)
    Code:
    LIST_ELEM *removePtr(LIST_ELEM *pHead, int loc) {
        LIST_ELEM *prevPtr, *nextPtr, *tempPtr;
        int size = sizeOf(pHead);
    
        if (loc == 0) {
            if (size == 0) return NULL; //already empty
            prevPtr = pHead->next;
            free(pHead);
            pHead = prevPtr;
            return pHead;
        }
    
        if (size < loc) return pHead; //can't delete element that doesn't exist
    
        //if we're still here then walk to the element and delete it
        tempPtr = pHead;
        for (int i = 0; i < loc; i++) {
            tempPtr = tempPtr->next;
        }
        prevPtr = nodeB4(pHead, loc); //if we didn't have the function, we'd walk prevPtr with tempPtr
        nextPtr = tempPtr->next;
        prevPtr->next = nextPtr;
        free(tempPtr);
        return pHead;
    }
    I've assumed you still don't want loc==0 to kill the whole list. EDIT: Also I haven't tested it, or even compiled it, so caveat lector.

  8. #38
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    ah hah hah thnx again tabstop. Its workin now. (sigh) what a work out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using free with a casted pointer..
    By smoking81 in forum C Programming
    Replies: 3
    Last Post: 10-01-2008, 04:50 PM
  2. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 4
    Last Post: 01-02-2008, 11:20 AM
  4. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM