Thread: does the parameters point to the same nodes??

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    does the parameters point to the same nodes??

    Code:
    item *what1(item *head, item *start, item *end)
    {
    	item *temp;
    	
    	if (head == start) 
    		head =end;
    	else{
    		temp=head;
    		while(temp->next != start)
    			temp= temp->next;
    		temp->next = end;
    	}
    	temp = start;
    	while(start!=end){
    		start=start->next;
    		temp->next=end->next;
    		end->next = temp;
    		temp=start;
    	}
    	return head;
    }
    
    item *what2(item *head){
    item *start, *end, *rest_list;
    start = head;
    
    while ((start!=NULL) && (start->next !=NULL)){
    	if(start->key =='-')
    		start=start->next;
    	else{
    		end=start;
    		while((end->next!=NULL) &&(end->next->key !='-'))
    			end=end->next;
    		rest_list = end->next;
    		head = what1(head,start, end);
    		start = rest_list;
    	}
    }
    while (end->next !=NULL) end = end->next;
    head = what1(head,head, end);
    return head;
    }
    i know that *ptr changes in the end of the what1
    astrix part of a pointer changes in the end of a function.
    so when i call what1,does the parameters point to the same place as before the what1 call,or the parameters change and point to the place in the end of what1
    ??

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int x;
    int *p1 = &x, *p2 = &x;
    
    if( p1 == p2 )
    {
        printf( "They point to the same place.\n" );
    }
    else
    {
        printf( "No.\n" );
    }
    Just like any other value holder, you can compare values to see if they're the same. If the value a pointer holds is the same as another pointer's value, they point to the same place.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am asking about

    to what the parameters will point in the end of what1 calling??
    on the one hand they are parameters and in the end
    they get their original precalling value.

    on the other hand
    in a function *ptr are saving their change.
    and because every line if changes -> which is *ptr. so its an astrix value
    and it has to save the change

    ??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This looks like homework to me.

    Function named something like "what1" or "problem1"? Check.
    Someone asking what it does? Check.

    Why don't you just pass it some information and find out what it's doing if you can't trace it by hand?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am not asking you to solve it
    i am saking you about what happens to the parameters

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Isn't that the same thing?

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no

    i am asking about

    to what the parameters will point in the end of what1 calling??
    on the one hand they are parameters and in the end
    they get their original precalling value.

    on the other hand
    in a function *ptr are saving their change.
    and because every line if changes -> which is *ptr. so its an astrix value
    and it has to save the change

    ??

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As many threads as we've had about pointers, let's do this one last time: a function can never change the value of something passed to it. Any changes to the value of head, start, and end will not be seen in the original function. (Obviously while the function is still going, you can do whatever you want -- but you are only dealing with a local copy.) Use that information to answer your question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  2. const Point& and const Point are same
    By noobcpp in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2007, 01:39 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. overloading operator help
    By syrel in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2004, 10:49 AM