-
hm you kind of lost me there.
So I have this list LIST_ELEM which holds a bunch of structs
pHead <-strct1<-strct2
and when I remove it i want it to look like
pHead<-struct2
or in the first case
pHead<-strct1
remove returns
pHead
but i'm not getting anything like that, I'm getting my original list when print it out, or find the size
-
so if I call a remove on my list which holds 1 item, shouldn't the size of the list after the remove return 0?
-
So run the following code and play "spot the parallel":
Code:
void func1(int bob) {
bob = 6;
}
void func2(int *bob) {
bob = NULL;
}
int main(void) {
int i = 11;
int *iptr = &i;
func1(i);
printf("%d\n", i);
func2(iptr);
if (iptr == NULL) printf("Variable successfully removed.\n");
printf("%p\n", iptr);
return 0;
}
-
11 and an address is returned.
-
so i basically can't set something to null and say it was removed.
-
Even though you pass in a pointer to the function, that means you can only change the value of what is pointed to, not the value of the pointer itself (that's just a copy). So if you want to change the value of the pointer, which it seems like you do, you'll have to add an extra *.
-
So if:
LIST_ELEM *ptr = pHead;
and I want to free the actual item at *ptr
i would have to use
free(**ptr)? doesn't that return an invalid type error?
-
No you would have to use
Code:
void removePtr(LIST_ELEM **pHead, int loc)
and adjust accordingly.
-
Man that would make it more difficult for me.
so taking in a *pHead and then setting equal to say
Code:
LIST_ELEM **ptr = pHead
-
You could do that but you're back where you started -- pHead wouldn't change just because you changed ptr.
I'm starting to worry a little bit -- all the list functions require a elem** to work (creating, adding, deleting, ...)
EDIT: And for that matter, it's not as though the change is hard: do a search-and-destroy turning pHead to *pHead everywhere in the function and you're done!
-
LOL you're beginning to worry me now. I've been using (LIST_ELEM *pHead) for my add, print, and find methods.
-
Well, okay, print just takes one star. Find could take one * if you're not trying to change the list. But if you add something to the front of the list and try to change pHead = just_created_element, that won't work.
-
so my program is a fluke since I use one * for add, and sizeOf returns the right size after adding in an element.
-
Are you in a special case where you are always adding at the end or something?
-
always adding to the front