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

    Join Date
    Jan 2009
    Posts
    118
    Rep Power
    0

    Why it keeps the change??


    in the end i have this line
    Code:
    head = newhead;
    head is not double astrix
    why it saves the change.
    it only saves changes for *head members
    ??

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    typedef struct node{
    	int value;
    	struct node *next;
    }Node;
    
    void what1(Node** p, int num){
    	Node *elt;
    	elt = (Node*)malloc (sizeof(Node));
    	elt -> next = *p;
    	elt ->value = num;
    	*p = elt;
    }
    
    Node* what2(Node *head){
    	Node *temp, *prev, *next, *prevmax, *newhead;	
    	while(head->next){
    		prevmax = prev = next = NULL;
    		for(temp=head; temp; temp=temp->next){
    			printf("%d ", temp->value);
    			next = temp -> next;
    			if((!prev || prev->value < temp->value) && 
    (!next || temp->value > next->value)){
    				if(! prevmax )
    					newhead = temp;
    				else
    					prevmax->next= temp;
    				prevmax = temp;
    			}
    			if(prev && prev!=prevmax)
    				free(prev);
    			prev = temp;
    		}
    		prevmax -> next = NULL;
    		head = newhead;
    		putchar('\n');
    	}
    	return head;
    }
    void main (){ 
    	int i, a[]={ 2, 4, 1};
    	Node* list=NULL;
    	for (i = 0; i < 3; i++)
    		what1(&list, a[i]);
    	list = what2(list);
    	printf("final value: %d ", list->value);
    	free(list);
    }
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally Posted by transgalactic
    head is not double astrix
    Double who? The glyph I presume you meant is an asterisk.

    Originally Posted by transgalactic
    why it saves the change.
    I think that you are probably misunderstanding what is happening. The function what2() finally returns head by copy, so in the call
    Code:
    list = what2(list);
    list is passed as the head argument, headis changed to point to newhead, and then returned and so copied to list.

    So list is not modified directly by what2(), but by main(), if assignment.

    You have been at this for a long time, and the light does not appear to be dawning for you does it!?

    Clifford
    Last edited by clifford; April 13th, 2009 at 03:35 PM. Reason: Self moderated
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    It doesn't save the change there. It returns the value of head though. So when you do this:
    Code:
    list = what2(list);
    then it gets assigned when what2 returns. If you'd done this:
    Code:
    tmplist = what2(list);
    then it will get assigned to tmplist.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    118
    Rep Power
    0
    ahh so head is a temporarily changed
    and it is used to return the temporary value
    but when the function ends
    it gets the old value
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally Posted by transgalactic
    but when the function ends
    it gets the old value
    No, head is an automatic variable, it is simply destroyed when what2() returns. The variable list in main() gets the new value.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    118
    Rep Power
    0
    in the main our list variable is changed because of the reurned value
    but in what2 list is not modified
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally Posted by transgalactic
    in the main our list variable is changed because of the reurned value
    but in what2 list is not modified
    Nobody suggested that it was. What is your point?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    118
    Rep Power
    0
    thanks i got the idea

IMN logo majestic logo threadwatch logo seochat tools logo