Confusing Pointer

This is a discussion on Confusing Pointer within the C Programming forums, part of the General Programming Boards category; I got this from a book i thought i knew pointers already until i saw this.. Compiled with MVC++ 4 ...

  1. #1
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204

    Confusing Pointer

    I got this from a book i thought i knew pointers already until i saw this..

    Compiled with MVC++
    4 warnings.. works fine though

    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    
     // What is this typedef to a pointer for? How does this work? 
    typedef struct listnode{
    	int data;
    	struct listnode *next;
    }*Listnode; 
    
    void printmenu();
    int insert( Listnode *, int );
    void printlist( Listnode  );
    
    int main()
    {
    	int mode = 0;
    	Listnode *start = NULL;
    	int data = 0;
    	//clrscr(); 
    	printmenu();
    
    	while( 1 ) {
    		printf( "\nEnter Command:" );
    		scanf( "%d", &mode );
    
    		if( mode == 1 ){
    			printf( "\n   Enter Data:" );
    			scanf( "%d", &data );
    			insert( &start, data );  // I got a warning here			
    printlist( start );
    		}
    		else if( mode == 2 ){
    		}
    		else return 0;
    	}
    
    	//getch();
    	return 0;
    }
    
    void printmenu()
    {
    	printf( "\n\n1 - Insert Data\n"
    			"2 - Delete Data\n"
    			"3 - Exit Simulation\n\n" );
    }
    
    int insert( Listnode *startPtr, int idata )
    {
    	Listnode newPtr,  prevPtr, curPtr;
    
    	newPtr = malloc( sizeof( Listnode ) );
    
    	newPtr->data = idata;
    	newPtr->next = NULL;
    
    	prevPtr = NULL;
    	curPtr = *startPtr;
    
    	while( curPtr != NULL && idata > curPtr->data ){
    		prevPtr = curPtr;
    		curPtr = curPtr->next;
    	}
    
    	if( prevPtr == NULL ){
      // this part within this bracket is kinda 
    confusing.. can someone explain this to me please? Lest say its 
    the first run of the program startPtr is currently pointing at 
    nothing... newPtr->next will be assign to point at starPtr, thats fine but then 
    startPtr points to newPtr right? hows that gonna work? Whats this line for? 
    Or will this make startPtr from main() to point at newPtr
     newPtr->next = *startPtr; 
    		*startPtr = newPtr;
    	}
    	else{
    		prevPtr->next = newPtr;
    		newPtr->next = curPtr;
    	}
    
    	return 0;
    }
    
    void printlist( Listnode startPtr )
    {
    	if( startPtr == NULL )
    		printf( "\n   The List is Empty (%d)\n", startPtr->data );
    	else{
    		while( startPtr != NULL ){
    			printf( "-->%d", startPtr->data );
    			startPtr = startPtr->next;
    		}
    		printf( "-->NULL\n" );
    	}
    }
    Last edited by loko; 08-29-2005 at 09:28 PM.

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    // What is this typedef to a pointer for? How does this work?
    Whenever you say ListNode, you actually mean struct listnode *, which is confusing because ListNode doesn't suggest that it's a pointer at all, and the rest of your code gets confused because of it. Change the struct declaration to this:
    Code:
    typedef struct listnode{
    	int data;
    	struct listnode *next;
    }Listnode;
    Then you can declare pointers of Listnode without getting tripped up by a hidden level of indirection. But that means you need to change the rest of the code to use dual indirection so that your changes stick when made inside a function:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    // What is this typedef to a pointer for? How does this work? 
    typedef struct listnode{
      int data;
      struct listnode *next;
    }Listnode; 
    
    void printmenu();
    int insert( Listnode **, int );
    void printlist( Listnode * );
    
    int main()
    {
      int mode = 0;
      Listnode *start = NULL;
      int data = 0;
    
      printmenu();
    
      while( 1 ) {
        printf( "\nEnter Command:" );
        scanf( "%d", &mode );
    
        if( mode == 1 ){
          printf( "\n   Enter Data:" );
          scanf( "%d", &data );
          insert( &start, data );
          printlist( start );
        }
        else if( mode == 2 ){
        }
        else return 0;
      }
    
      return 0;
    }
    
    void printmenu()
    {
      printf( "\n\n1 - Insert Data\n"
        "2 - Delete Data\n"
        "3 - Exit Simulation\n\n" );
    }
    
    int insert( Listnode **startPtr, int idata )
    {
      Listnode *newPtr,  *prevPtr, *curPtr;
    
      newPtr = malloc( sizeof( Listnode ) );
    
      newPtr->data = idata;
      newPtr->next = NULL;
    
      prevPtr = NULL;
      curPtr = *startPtr;
    
      while( curPtr != NULL && idata > curPtr->data ){
        prevPtr = curPtr;
        curPtr = curPtr->next;
      }
    
      if( prevPtr == NULL ){
        newPtr->next = *startPtr; 
        *startPtr = newPtr;
      }
      else{
        prevPtr->next = newPtr;
        newPtr->next = curPtr;
      }
    
      return 0;
    }
    
    void printlist( Listnode *startPtr )
    {
      if( startPtr == NULL )
        printf( "\n   The List is Empty (%d)\n", startPtr->data );
      else{
        while( startPtr != NULL ){
          printf( "-->%d", startPtr->data );
          startPtr = startPtr->next;
        }
        printf( "-->NULL\n" );
      }
    }
    this part within this bracket is kinda confusing.. can someone explain this to me please? Lest say its the first run of the program startPtr is currently pointing at nothing... newPtr-> will be pointing at starPtr, thats fine but then startPtr points to new Ptr right? hows that gonna work?
    That code sticks the new node into the front of the list by pointing the new node to the current front of the list, then setting the front of the list to point to the new node.
    Last edited by Narf; 08-29-2005 at 09:32 PM.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Code:
      
      if( prevPtr == NULL ){
        newPtr->next = *startPtr; 
        *startPtr = newPtr; // This will make *start from main() point to newPtr too? hows that?     }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They're using a pointer to a pointer. It's easy enough if you remember this concept: In order to change something you pass to a function, you need to instead pass a pointer to that item. For example, if you have an integer outside of a function, and you wish to keep whatever changes happen inside the function, you use a pointer to it. Like so:
    Code:
    void foo( int *bar )
    {
        *bar = 10;
    }
    ...
    
    int x;
    
    foo( &x ); /* x's address is passed to the function, so it knows where it is */
    That's in a nutshell what's happening. You pass the address of an object, so that you can use it to change the origional. This applies to pointers too. If you want to change what a pointer points to, like we changed what x contains, we pass a pointer to it. Same concept:
    Code:
    void foo( int **bar )
    {
        *bar = somenewpointer;
    }
    ...
    int *xptr;
    
    foo( &xptr ); /* allow xptr to be changed inside the function */

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

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Ahh i dindnt know i could do that.. thanks to both

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 12:26 AM
  2. confusing char pointer
    By vaibhav in forum C Programming
    Replies: 7
    Last Post: 08-29-2005, 08:45 PM
  3. Pointer To Functions In COM Confusing
    By bartybasher in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2004, 04:08 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 06:40 PM
  5. Replies: 2
    Last Post: 11-22-2001, 12:22 PM

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