double pointers

This is a discussion on double pointers within the C Programming forums, part of the General Programming Boards category; can someone please explain me the concept of double pointers refering to the following code or any other code that ...

  1. #1
    esi
    esi is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    double pointers

    can someone please explain me the concept of double pointers refering to the following code or any other code that you think might help me clear my concept for double pointers.
    Code:
    struct Node
    {
    	int value;
    	struct Node*Next;
    };
    
    case 1:
    			printf("\nEnter the number to insert:\n");
    			scanf("%d",&val);
    			insert (&start, val);
    			break;
    	
    			case 2:
    			remove2(&start);
    			break;
    
    
    
    void insert (struct Node**start, int v)
    {
    	if (*start == NULL)
    	{
    		*start = (struct Node*) malloc(sizeof(struct Node));
    		(*start)-> value = v;
    		(*start)-> Next = NULL;
    	}
    	else{
    		struct Node*temp = *start;
    		while ((temp-> Next) != NULL)
    		{
    			temp = temp->Next;
    		}
    		temp->Next= (struct Node*) malloc (sizeof(struct Node));
    		temp = temp-> Next;
    		temp->value= v;
    		temp-> Next = NULL;
    	}
    }
    
    
    void remove2 (struct Node**start)
    {
    	if( *start == NULL)
    	{                                   
    		printf("\nThe List is Empty\n");
    	}
    	else
    	{
    		struct Node*temp= *start;
    		while ((temp->Next->Next) != NULL)
    		{
    			temp = temp->Next;
    		}
    	temp->Next = NULL;
    	}
    }

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Well, there are several possibilities when a double pointer makes sense. Suppose you pass an integer to a function and you want to modify its value, then you usually write:

    Code:
    void myFunc(int *myNumber)
    {
        *myNumber = 10;
    }
    But now image that the type you want to modify is not an integer but already a pointer, then you need a pointer to that pointer:

    Code:
    void myFunc(int **myNumber, int *somePointer)
    {
        *myNumber = somePointer;
    }
    Or when you have an array of strings, since a string is already of type char*, an array of strings is of type char**.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. calculations with pointers
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-17-2007, 01:46 PM
  4. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21