POINTER to POINTER Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nosnibor
    New Member
    • Oct 2007
    • 10

    POINTER to POINTER Problem

    Need some help with this pointer problem.
    Code:
    void Function( ADT **a/*NULL*/,  ADT *b /*NOT NULL POINT TO SOME DATA*/)
    {
    	
        ADT *c = NULL;
       
       if( a == NULL) 
       {
             *a  = b;                                              //ERROR READ OF ADDRESS VIOLATION
    
    .
    .
    .
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by Nosnibor
    [code=c]
    if( a == NULL)
    {
    *a = b; //ERROR READ OF ADDRESS VIOLATION

    [/code]
    You just checked to see if a was NULL and if it was assign to were it was pointing. You have deferenced the NULL pointer always a bad idea.

    Perhaps you meant

    [code=c]
    if( a != NULL)
    {
    *a = b;
    }
    [/code]

    or perhaps you meant

    [code=c]
    if( a != NULL && *a == NULL)
    {
    *a = b;
    }
    [/code]

    Comment

    • Nosnibor
      New Member
      • Oct 2007
      • 10

      #3
      Think this, what I came up with...

      Code:
      void SOMEFUNCTION( ADT **a, ADT **b USERS *c )
      {
      	ADT *p,
         		*t;
      
      	if(b == (ADT **)NULL) 				
              {
                    a->next = (ADT*)NULL;
          	      b->prev = (ADT*)NULL;
                    *a = c ; 	/////*******/////
                    *b= c;		////*********/////
          	return;
        	}
            t = (*at);
            p = (ADT *)NULL;
      
            /*some other codes*/

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by Nosnibor
        Think this, what I came up with...

        Code:
        void SOMEFUNCTION( ADT **a, ADT **b USERS *c )
        {
        	ADT *p,
           		*t;
        
        	if(b == (ADT **)NULL) 				
                {
                      a->next = (ADT*)NULL;
            	      b->prev = (ADT*)NULL;
                      *a = c ; 	/////*******/////
                      *b= c;		////*********/////
            	return;
          	}
              t = (*at);
              p = (ADT *)NULL;
        
              /*some other codes*/
        I would be really surprised it this compiled with errors and warnings so perhaps you'd better post what you get.

        If it did compile I would be surprised if it ran(consistentl y) without crashing.

        Comment

        • Nosnibor
          New Member
          • Oct 2007
          • 10

          #5
          Code:
          /*Create a doubly linked*/
          
          void store(  struct address *i  /* new element */, struct address **start /* first element in list */,  struct address **last /* last element in list */ )
          {
            struct address *old, *p;
          
            if(*last==NULL) {  /* first element in list */
              i->next = NULL;
              i->prior = NULL;
              *last = i;                  /*HERE WRITE OF ADDRESS ERROR*/
              *start = i;                 /*HERE WRITE OF ADDRESS*/
              return;
            }
            p = *start;                   /* start at top of list */
          /*CODE CONTINUES*/

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by Nosnibor
            Code:
            /*Create a doubly linked*/
            
            void store(  struct address *i  /* new element */, struct address **start /* first element in list */,  struct address **last /* last element in list */ )
            {
              struct address *old, *p;
            
              if(*last==NULL) {  /* first element in list */
                i->next = NULL;
                i->prior = NULL;
                *last = i;                  /*HERE WRITE OF ADDRESS ERROR*/
                *start = i;                 /*HERE WRITE OF ADDRESS*/
                return;
              }
              p = *start;                   /* start at top of list */
            /*CODE CONTINUES*/
            Well this looks correct, so if you are getting errors it may be how you are calling it.

            Comment

            • Nosnibor
              New Member
              • Oct 2007
              • 10

              #7
              Ok Thanks. But Still lost in pointers. So lets say

              Code:
              void main()
              {
              
                ADT 	*Head = NULL,*End = NULL;
              
              
               ANYFUNCTION(  Head, End );
              .
              .
              .
              Code:
              void ANYFUNCTION( ADT  *A, ADT *B )
              {
               /*Performs some operations between ere*/
              .
              .
              .
              /*here*/
                        
              SOMEOTHERFUNCTION( &A , &B  );
              
              }
              Code:
               
              void SOMEOTHERFUNCTION( **C , **D )
               /*Performs some operations between ere*/
              .
              .
              .
              /*here*/
              return;
              }
              My question is how do I get Head and End to reflect the changes made to A and B?
              With using globals.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                You could pass those pointers 'by reference', or you could pass the address of the pointers in each case. If you choose the former, it's very easy - just put the & symbol after the type name in your function headers. If you choose the latter, you have to change your function headers to accept double pointers (and triple pointers, for SOMEOTHERFUNCTI ON), and work around dereferencing twice (or three times) in your function.

                Comment

                • Nosnibor
                  New Member
                  • Oct 2007
                  • 10

                  #9
                  Originally posted by Nosnibor
                  Ok Thanks. But Still lost in pointers. So lets say

                  Code:
                  void main()
                  {
                  
                    ADT 	*Head = NULL,*End = NULL;
                  
                  
                   ANYFUNCTION(  Head, End );
                  .
                  .
                  .
                  Code:
                  void ANYFUNCTION( ADT  *A, ADT *B )
                  {
                   /*Performs some operations between ere*/
                  .
                  .
                  .
                  /*here*/
                            
                  SOMEOTHERFUNCTION( &A , &B  );
                  
                  }
                  Code:
                   
                  void SOMEOTHERFUNCTION( **C , **D )
                   /*Performs some operations between ere*/
                  .
                  .
                  .
                  /*here*/
                  return;
                  }
                  My question is how do I get Head and End to reflect the changes made to A and B?
                  With using globals.

                  Code:
                  void main()
                  {
                  
                    ADT 	*Head = NULL,*End = NULL;
                  
                  
                   ANYFUNCTION(  &Head, &End );
                  .
                  .
                  .
                  Code:
                  void ANYFUNCTION( ADT  **A, ADT **B )
                  {
                   /*Performs some operations between ere*/
                  .
                  .
                  .
                  /*here*/
                            
                  SOMEOTHERFUNCTION( A , B  );
                  
                  }
                  Code:
                   
                  void SOMEOTHERFUNCTION( **C , **D )
                   /*Performs some operations between ere*/
                  .
                  .
                  .
                  /*here*/
                  return;
                  }
                  I made the following changes and they seem work seemly. Thanks

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Note

                    [code=c]void main()
                    {
                    <code here>
                    }
                    [/code]

                    is quite wrong and could cause you program to function incorrectly. main always returns int

                    [code=c]int main()
                    {
                    <code here>

                    return 0;
                    }
                    [/code]

                    Comment

                    • Nosnibor
                      New Member
                      • Oct 2007
                      • 10

                      #11
                      Originally posted by Banfa
                      Note

                      [code=c]void main()
                      {
                      <code here>
                      }
                      [/code]

                      is quite wrong and could cause you program to function incorrectly. main always returns int

                      [code=c]int main()
                      {
                      <code here>

                      return 0;
                      }
                      [/code]
                      Do not understand what you are saying in post could you be a bit more vivid?
                      Are you saying a void main would clause this not operate correctly?
                      And if so why?
                      What is the difference with a void main with no return, and a int main with a return? What does main actually returns to?

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Originally posted by Nosnibor
                        Do not understand what you are saying in post could you be a bit more vivid?
                        Are you saying a void main would clause this not operate correctly?
                        And if so why?
                        What is the difference with a void main with no return, and a int main with a return? What does main actually returns to?
                        Oops sorry should have said

                        Yes using void main could cause your code to work incorrectly, it is wrong.

                        Using void main invokes undefined behaviour (because the C/C++ standard says so). Undefined behaviour is bad because the program and quite literally do anything (although the most common results are incorrect calculations, program crashes and appearing to work without a problem).

                        main returns to the c start-up code and the difference is that this start-up code is expecting main to return int not return void.

                        Comment

                        Working...