Thread: Changing the value of something through a double derefence

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    24

    Changing the value of something through a double derefence

    I was talking to someone earlier about how to change the value of something from a function, and they said what was needed was to use a ** to change something, and was wondering if I could get a walk - through of what happens. I understand a single pointer well enough, but a pointer through a pointer is kind of confusing to me. Here is a simple example.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE  5
    int add(int ** TOP, int * stack);
    
    int *stack = NULL;
        int *TOP = NULL;
        stack = (int *)malloc (SIZE * sizeof(int));
        TOP = stack;
        int b = 0; 
        int (*f[])(int ** TOP ,int * stack) = {add, pop};
        
        while (b < 3)
        {
            printf("The address that TOP points to is %d\n", TOP);
            printf("What would you like to do?");
            scanf("%d", &b);
            (*f[b])( &TOP , &stack);
            printf("The address that TOP points to is %d\n", TOP);
            }
    
    int add(int ** TOP, int * stack)
    {
          int a = 0;
           
          printf("The address that TOP points to is %d\n", TOP);
          printf("Please enter a number to stack, push 0 to quit this operation.");
          scanf("%d", &a);
          *TOP = a;
          printf("The number put on the stack was %d\n", **TOP);
          **TOP ++;
    Why is it that when the program prints the address of TOP in main, it is different than the address of TOP in the function? Is it because it is a different instance of TOP because it is in the function? When I put the number on *TOP, and come out of the function back to main, it then says the adress of TOP is the number entered into *TOP, and am not sure why. And the **TOP ++ at the end I am thinking it increments malloc by 1, therefore bringing the pointer TOP up to point at the next element, or am I completely off base there? Thank you for the help

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    For a function to change the value of a parameter in the caller it needs a pointer. That's it. The only reason to pass a double pointer is if the thing you want to change is itself a pointer. It looks like you just need a single pointer.

    EDIT: I didn't look at your program very carefully. I see what's up now, but you haven't even posted your main so I can't answer your question. Post the whole thing.
    Last edited by oogabooga; 10-30-2013 at 09:09 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    My problem is I need to be able to change the TOP value in main from the function so I can tell if I am out of bounds with what is set up by malloc.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You've only posted an uncompilable fragment. Can you post the whole thing?

    EDIT: Arghhh, okay, I read it carefully this time.

    Yes, it's a new copy of TOP in the function.

    When you assign an integer to it, you need to use **TOP = a.

    But when you increment the pointer, you need to do ++*TOP.

    So you're basically doing it backwards.
    Last edited by oogabooga; 10-30-2013 at 09:12 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    That is the part that is confusing me. When I pass in **TOP to the function, which value am I actually passing in? The value of the 1st space malloc is in?

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're passing in the address of the stack variable, and the stack variable points to the malloced space. *TOP gives you the stack pointer. **TOP gives you the ints that it points to. To set one of the ints, you use **TOP = x. To increment the stack pointer, you use ++*TOP.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    I am having problems with the checking of the bounds of malloc. I have an if statement in my function saying if the address of top equals the last part of malloc, to print that the stack is full. When I test it, though, it just keeps going past it. Any idea why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE  5
    int add(int ** top, int * stack);
    int pop(int **top, int * stack);
    
    
    
    
    int main()
    {
    	int *stack = NULL;
    	int *top = NULL;
    	stack = (int *)malloc (SIZE * sizeof(int));
    	top = stack;
        int b = 0; 
    	int (*f[])(int ** top ,int * stack) = {add, pop};
    	
    	while (b < 2)
    	{
    		printf("The address of stack is %d\n", stack + 5);
    		printf("What would you like to do?\n");
    		scanf("%d", &b);
    		f[b]( &top , &stack);
    		printf("The address that TOP points to is %d\n", top);
    	}
    	
    }
    
    
    int add(int ** top, int * stack)
    {
    	  int a = 0;
    	  if ( *top == stack + 5)
    	  {
    	  
    	    printf("The address of *top is %d ", *top);
    	  	printf("The stack is full.");
    	  }
    	   else
    	   
    	   {
    	   
    	  printf("The address that TOP points to is %d\n", *top);
    	  printf("Please enter a number to stack, push 0 to quit this operation.");
    	  scanf("%d", &a);
    	  **top = a;
    	  printf("The number put on the stack was %d\n", **top);
    	   ++ *top;
    	   printf("the address of top is now %d\n", *top);
        	}
    	        
    	        
    	        
    		}
    	
    
    
    int pop(int **top, int  * stack)
    {
    	if (*top == *stack - 1)
    		printf("The stack is already empty.\n");
    	else
    	{
    	
    	   printf("The number that was just popped was %d", **top);
    	    -- *top;
    	}
    }

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    top and stack in main have same type - int*
    You pass to your function &top and &stack thus both parameters should be int**

    You have declared functions with int **top, int * stack parameters

    Further in one function you have

    *top == *stack

    in another

    *top == stack

    This all does not mix too good together
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invalid types 'double*[double]' for array subscript
    By Elvio Esposito in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2013, 11:47 AM
  2. changing a string to a 'double' data type
    By Brian_Jones in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2009, 05:59 PM
  3. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  4. Changing double time to double cost
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 10:29 AM
  5. Changing single to double linked list
    By BR7 in forum C Programming
    Replies: 1
    Last Post: 09-06-2002, 07:21 PM