Passing lists to functions

This is a discussion on Passing lists to functions within the C Programming forums, part of the General Programming Boards category; hi again guys.half of the code is working now,but still struggling with pointers.my next problem now is passing the *front ...

  1. #16
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    hi again guys.half of the code is working now,but still struggling with pointers.my next problem now is passing the *front and *rear
    second time in a function.

    i want to use the enqueue function in a other function (for example,showqueue, a function that dequeues-prints-enqueues).

    Code:
    void showqueue(int qsiz,struct node **fro,struct node **rea)
    {
     int q,va;
     struct node *tmpee;
     for (q=0;q<qsiz;q++)
     {
      dequeue(&va,&fro,&rea,&qsiz);
      if (q==0)
       printf("[%d]<--front\n",va);
      else if (q==qsiz)
       printf("[%d]<--rear",va); 
      else
       printf("[%d]\n",va); 
       tmpee=newnode(va);
      enqueue(&fro,&rea,tmpee,&qsiz);
     }
    
    }
    Any ideas?should i give up with all this?totally wrong?

  2. #17
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    How does your dequeue() function look like and with each function call you are increasing the level of indirection.
    You might be better off just using a single level of indirection ie struct node *fro instead of struct ***node fro

  3. #18
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    i did not posted it to keep the code size small, since this is clearly a pointer syntax question but ok here it is.it works without problems now :

    Code:
    int dequeue(int *it,struct node **frr,struct node **rre,int *qsz)
    {
     struct node *tmpe;  
     int dfl=0;
     if (*frr!=*rre)
     {
      *it=(*frr)->info;
      tmpe=*frr;             
      *frr=(*frr)->next;  
      free(tmpe);
      *qsz--;
      dfl=1;
     }
     else if ((*frr!=NULL) && (*frr==*rre))
     {
      *it=(*frr)->info;
      tmpe=*frr;            
      *frr=NULL;
      *rre=NULL;
      free(tmpe);
      *qsz=0;
      dfl=1; 
     }
     return dfl;
    }
    i am allowed only to use enqueue-dequeue to show the items of the queue.so is it possible to increase that level of indirection one more level further?

  4. #19
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Why do you want add another level of indirection if all's well with the program??

  5. #20
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    nope there is a problem when i call enqueue and dequeue function in the showqueue.the **fro must pass to the two functions somehow and i cannot think of anything.

    maybe you will understand my question from this:

    1st level: main(): *front (pointer for structure)
    2nd level showqueue: **fro (pointer of pointer of structure in order to be used in the function showqueue)
    3rd level enqueue (called in showqueue): ??? (pointer of pointer of pointer of structure in order to be passed from showqueue to enqueue)

    tried *&fro,&fro,&*fro in showqueue to pass the double pointer to enqueue but nothing worked.
    did you understand where is the problem?

  6. #21
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Quote Originally Posted by Griever View Post
    nope there is a problem when i call enqueue and dequeue function in the showqueue.the **fro must pass to the two functions somehow and i cannot think of anything.

    maybe you will understand my question from this:

    1st level: main(): *front (pointer for structure)
    From main() call enqueue() using struct node *fro.
    Quote Originally Posted by Griever View Post
    2nd level showqueue: **fro (pointer of pointer of structure in order to be used in the function showqueue)
    You can again pass struct node *fro to showqueue; no need to pass struct node **fro
    Quote Originally Posted by Griever View Post
    3rd level enqueue (called in showqueue): ??? (pointer of pointer of pointer of structure in order to be passed from showqueue to enqueue)
    Again pass struct node *fro to enqueue instead of struct node ***fro.
    Quote Originally Posted by Griever View Post
    tried *&fro,&fro,&*fro in showqueue to pass the double pointer to enqueue but nothing worked.
    How about simply struct node *fro.
    Quote Originally Posted by Griever View Post
    did you understand where is the problem?
    Trying...others may have better insights / ideas.

  7. #22
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    ah yes you are absolutely right... i wanted to convert my code in using pointers functioning like when i was using global variables that i did not make a simple check of what is actually needed to be pointed...i corrected almost everything.also i found that *qsz++ or *qsz-- did not work and i corrected it putting the ++ change in a variable (qq++; *qsz =qq and updating the pointer at the end of the process.thanks again for your quick responses and your time.

  8. #23
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Quote Originally Posted by Griever View Post
    ah yes you are absolutely right... i wanted to convert my code in using pointers functioning like when i was using global variables that i did not make a simple check of what is actually needed to be pointed...i corrected almost everything.also i found that *qsz++ or *qsz-- did not work and i corrected it putting the ++ change in a variable (qq++; *qsz =qq and updating the pointer at the end of the process.thanks again for your quick responses and your time.
    IMHO that is the correct approach; keep it simple and call each function with a single level of indirection ie struct node *fro.
    *qs++ or *qs-- won't work as it increments or decrements the pointer qs not the object it points to; use (*qs)++ or (*qs)-- instead.
    Last edited by itCbitC; 11-14-2008 at 04:48 PM.

  9. #24
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    i upload the final version of my code to anyone who is interested(i changed every message and comment to english).could not have done it without your help.

    code contains DOS commands ( system("PAUSE"); ,system("CLS"); )so it will not compile on non-windows environments.delete system("CLS"); and replace system("PAUSE"); with getchar(); to fix this.

    thanks a lot.
    Attached Files Attached Files

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing from functions in classes to another function in another class?
    By Robert_Sitter in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2005, 07:46 AM
  2. structure question: passing to functions
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 03-07-2005, 08:58 PM
  3. Replies: 6
    Last Post: 05-06-2003, 04:08 PM
  4. Passing structures between functions
    By TankCDR in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 10:54 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

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