POINTER to POINTER Problem  | Newbie | | Join Date: Oct 2007
Posts: 10
| |
Need some help with this pointer problem. -
-
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
-
-
.
-
.
-
.
-
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,169
| | | re: POINTER to POINTER Problem Quote:
Originally Posted by Nosnibor -
if( a == NULL)
-
{
-
*a = b; //ERROR READ OF ADDRESS VIOLATION
-
-
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 -
if( a != NULL)
-
{
-
*a = b;
-
}
-
or perhaps you meant -
if( a != NULL && *a == NULL)
-
{
-
*a = b;
-
}
-
|  | Newbie | | Join Date: Oct 2007
Posts: 10
| | | re: POINTER to POINTER Problem
Think this, what I came up with... -
-
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*/
-
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,169
| | | re: POINTER to POINTER Problem Quote:
Originally Posted by Nosnibor Think this, what I came up with... -
-
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(consistently) without crashing.
|  | Newbie | | Join Date: Oct 2007
Posts: 10
| | | re: POINTER to POINTER Problem - /*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*/
-
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,169
| | | re: POINTER to POINTER Problem Quote:
Originally Posted by Nosnibor - /*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.
|  | Newbie | | Join Date: Oct 2007
Posts: 10
| | | re: POINTER to POINTER Problem
Ok Thanks. But Still lost in pointers. So lets say -
void main()
-
{
-
-
ADT *Head = NULL,*End = NULL;
-
-
-
ANYFUNCTION( Head, End );
-
.
-
.
-
.
-
-
void ANYFUNCTION( ADT *A, ADT *B )
-
{
-
/*Performs some operations between ere*/
-
.
-
.
-
.
-
/*here*/
-
-
SOMEOTHERFUNCTION( &A , &B );
-
-
}
-
-
-
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.
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | 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.
|  | Newbie | | Join Date: Oct 2007
Posts: 10
| | | re: POINTER to POINTER Problem Quote:
Originally Posted by Nosnibor Ok Thanks. But Still lost in pointers. So lets say -
void main()
-
{
-
-
ADT *Head = NULL,*End = NULL;
-
-
-
ANYFUNCTION( Head, End );
-
.
-
.
-
.
-
-
void ANYFUNCTION( ADT *A, ADT *B )
-
{
-
/*Performs some operations between ere*/
-
.
-
.
-
.
-
/*here*/
-
-
SOMEOTHERFUNCTION( &A , &B );
-
-
}
-
-
-
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. -
void main()
-
{
-
-
ADT *Head = NULL,*End = NULL;
-
-
-
ANYFUNCTION( &Head, &End );
-
.
-
.
-
.
-
-
void ANYFUNCTION( ADT **A, ADT **B )
-
{
-
/*Performs some operations between ere*/
-
.
-
.
-
.
-
/*here*/
-
-
SOMEOTHERFUNCTION( A , B );
-
-
}
-
-
-
void SOMEOTHERFUNCTION( **C , **D )
-
/*Performs some operations between ere*/
-
.
-
.
-
.
-
/*here*/
-
return;
-
}
-
I made the following changes and they seem work seemly. Thanks
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,169
| | | re: POINTER to POINTER Problem
Note - void main()
-
{
-
<code here>
-
}
-
is quite wrong and could cause you program to function incorrectly. main always returns int - int main()
-
{
-
<code here>
-
-
return 0;
-
}
-
|  | Newbie | | Join Date: Oct 2007
Posts: 10
| | | re: POINTER to POINTER Problem Quote:
Originally Posted by Banfa Note - void main()
-
{
-
<code here>
-
}
-
is quite wrong and could cause you program to function incorrectly. main always returns int - int main()
-
{
-
<code here>
-
-
return 0;
-
}
-
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?
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,169
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,439 network members.
|