473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

POINTER to POINTER Problem

Nosnibor
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.  
Oct 29 '07 #1
11 1430
Banfa
9,065 Expert Mod 8TB
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.  
Oct 29 '07 #2
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.  
Oct 30 '07 #3
Banfa
9,065 Expert Mod 8TB
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.
Oct 30 '07 #4
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.  
Oct 30 '07 #5
Banfa
9,065 Expert Mod 8TB
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.
Oct 30 '07 #6
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.
Oct 31 '07 #7
Ganon11
3,652 Expert 2GB
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.
Oct 31 '07 #8
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
Nov 2 '07 #9
Banfa
9,065 Expert Mod 8TB
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.  
Nov 2 '07 #10
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?
Nov 2 '07 #11
Banfa
9,065 Expert Mod 8TB
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.
Nov 2 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
9
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value...
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.