Connecting Tech Pros Worldwide Forums | Help | Site Map

POINTER to POINTER Problem

Nosnibor's Avatar
Newbie
 
Join Date: Oct 2007
Posts: 10
#1: Oct 29 '07
Need some help with this pointer problem.
Expand|Select|Wrap|Line Numbers
  1.  
  2. void Function( ADT **a/*NULL*/,  ADT *b /*NOT NULL POINT TO SOME DATA*/)
  3. {
  4.  
  5.     ADT *c = NULL;
  6.  
  7.    if( a == NULL) 
  8.    {
  9.          *a  = b;                                              //ERROR READ OF ADDRESS VIOLATION
  10.  
  11. .
  12. .
  13. .
  14.  

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#2: Oct 29 '07

re: POINTER to POINTER Problem


Quote:

Originally Posted by Nosnibor

Expand|Select|Wrap|Line Numbers
  1.    if( a == NULL) 
  2.    {
  3.          *a  = b;          //ERROR READ OF ADDRESS VIOLATION
  4.  
  5.  

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

Expand|Select|Wrap|Line Numbers
  1.    if( a != NULL) 
  2.    {
  3.          *a  = b;
  4.    }
  5.  
or perhaps you meant

Expand|Select|Wrap|Line Numbers
  1.    if( a != NULL && *a == NULL) 
  2.    {
  3.          *a  = b;
  4.    }
  5.  
Nosnibor's Avatar
Newbie
 
Join Date: Oct 2007
Posts: 10
#3: Oct 30 '07

re: POINTER to POINTER Problem


Think this, what I came up with...

Expand|Select|Wrap|Line Numbers
  1.  
  2. void SOMEFUNCTION( ADT **a, ADT **b USERS *c )
  3. {
  4.     ADT *p,
  5.            *t;
  6.  
  7.     if(b == (ADT **)NULL)                 
  8.         {
  9.               a->next = (ADT*)NULL;
  10.               b->prev = (ADT*)NULL;
  11.               *a = c ;     /////*******/////
  12.               *b= c;        ////*********/////
  13.         return;
  14.       }
  15.       t = (*at);
  16.       p = (ADT *)NULL;
  17.  
  18.       /*some other codes*/
  19.  
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#4: Oct 30 '07

re: POINTER to POINTER Problem


Quote:

Originally Posted by Nosnibor

Think this, what I came up with...

Expand|Select|Wrap|Line Numbers
  1.  
  2. void SOMEFUNCTION( ADT **a, ADT **b USERS *c )
  3. {
  4.     ADT *p,
  5.            *t;
  6.  
  7.     if(b == (ADT **)NULL)                 
  8.         {
  9.               a->next = (ADT*)NULL;
  10.               b->prev = (ADT*)NULL;
  11.               *a = c ;     /////*******/////
  12.               *b= c;        ////*********/////
  13.         return;
  14.       }
  15.       t = (*at);
  16.       p = (ADT *)NULL;
  17.  
  18.       /*some other codes*/
  19.  

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(consistently) without crashing.
Nosnibor's Avatar
Newbie
 
Join Date: Oct 2007
Posts: 10
#5: Oct 30 '07

re: POINTER to POINTER Problem


Expand|Select|Wrap|Line Numbers
  1. /*Create a doubly linked*/
  2.  
  3. void store(  struct address *i  /* new element */, struct address **start /* first element in list */,  struct address **last /* last element in list */ )
  4. {
  5.   struct address *old, *p;
  6.  
  7.   if(*last==NULL) {  /* first element in list */
  8.     i->next = NULL;
  9.     i->prior = NULL;
  10.     *last = i;                  /*HERE WRITE OF ADDRESS ERROR*/
  11.     *start = i;                 /*HERE WRITE OF ADDRESS*/
  12.     return;
  13.   }
  14.   p = *start;                   /* start at top of list */
  15. /*CODE CONTINUES*/
  16.  
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#6: Oct 30 '07

re: POINTER to POINTER Problem


Quote:

Originally Posted by Nosnibor

Expand|Select|Wrap|Line Numbers
  1. /*Create a doubly linked*/
  2.  
  3. void store(  struct address *i  /* new element */, struct address **start /* first element in list */,  struct address **last /* last element in list */ )
  4. {
  5.   struct address *old, *p;
  6.  
  7.   if(*last==NULL) {  /* first element in list */
  8.     i->next = NULL;
  9.     i->prior = NULL;
  10.     *last = i;                  /*HERE WRITE OF ADDRESS ERROR*/
  11.     *start = i;                 /*HERE WRITE OF ADDRESS*/
  12.     return;
  13.   }
  14.   p = *start;                   /* start at top of list */
  15. /*CODE CONTINUES*/
  16.  

Well this looks correct, so if you are getting errors it may be how you are calling it.
Nosnibor's Avatar
Newbie
 
Join Date: Oct 2007
Posts: 10
#7: Oct 31 '07

re: POINTER to POINTER Problem


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

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  
  4.   ADT     *Head = NULL,*End = NULL;
  5.  
  6.  
  7.  ANYFUNCTION(  Head, End );
  8. .
  9. .
  10. .
  11.  
Expand|Select|Wrap|Line Numbers
  1. void ANYFUNCTION( ADT  *A, ADT *B )
  2. {
  3.  /*Performs some operations between ere*/
  4. .
  5. .
  6. .
  7. /*here*/
  8.  
  9. SOMEOTHERFUNCTION( &A , &B  );
  10.  
  11. }
  12.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. void SOMEOTHERFUNCTION( **C , **D )
  3.  /*Performs some operations between ere*/
  4. .
  5. .
  6. .
  7. /*here*/
  8. return;
  9. }
  10.  
My question is how do I get Head and End to reflect the changes made to A and B?
With using globals.
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#8: Oct 31 '07

re: POINTER to POINTER Problem


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 SOMEOTHERFUNCTION), and work around dereferencing twice (or three times) in your function.
Nosnibor's Avatar
Newbie
 
Join Date: Oct 2007
Posts: 10
#9: Nov 2 '07

re: POINTER to POINTER Problem


Quote:

Originally Posted by Nosnibor

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

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  
  4.   ADT     *Head = NULL,*End = NULL;
  5.  
  6.  
  7.  ANYFUNCTION(  Head, End );
  8. .
  9. .
  10. .
  11.  
Expand|Select|Wrap|Line Numbers
  1. void ANYFUNCTION( ADT  *A, ADT *B )
  2. {
  3.  /*Performs some operations between ere*/
  4. .
  5. .
  6. .
  7. /*here*/
  8.  
  9. SOMEOTHERFUNCTION( &A , &B  );
  10.  
  11. }
  12.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. void SOMEOTHERFUNCTION( **C , **D )
  3.  /*Performs some operations between ere*/
  4. .
  5. .
  6. .
  7. /*here*/
  8. return;
  9. }
  10.  
My question is how do I get Head and End to reflect the changes made to A and B?
With using globals.


Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  
  4.   ADT     *Head = NULL,*End = NULL;
  5.  
  6.  
  7.  ANYFUNCTION(  &Head, &End );
  8. .
  9. .
  10. .
  11.  
Expand|Select|Wrap|Line Numbers
  1. void ANYFUNCTION( ADT  **A, ADT **B )
  2. {
  3.  /*Performs some operations between ere*/
  4. .
  5. .
  6. .
  7. /*here*/
  8.  
  9. SOMEOTHERFUNCTION( A , B  );
  10.  
  11. }
  12.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. void SOMEOTHERFUNCTION( **C , **D )
  3.  /*Performs some operations between ere*/
  4. .
  5. .
  6. .
  7. /*here*/
  8. return;
  9. }
  10.  
I made the following changes and they seem work seemly. Thanks
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#10: Nov 2 '07

re: POINTER to POINTER Problem


Note

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3. <code here>
  4. }
  5.  
is quite wrong and could cause you program to function incorrectly. main always returns int

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. <code here>
  4.  
  5.     return 0;
  6. }
  7.  
Nosnibor's Avatar
Newbie
 
Join Date: Oct 2007
Posts: 10
#11: Nov 2 '07

re: POINTER to POINTER Problem


Quote:

Originally Posted by Banfa

Note

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3. <code here>
  4. }
  5.  
is quite wrong and could cause you program to function incorrectly. main always returns int

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. <code here>
  4.  
  5.     return 0;
  6. }
  7.  

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?
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#12: Nov 2 '07

re: POINTER to POINTER Problem


Quote:

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.
Reply