Thread: Poiter to a pointer

  1. #1
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Unhappy Poiter to a pointer

    What is the problem with my display function....
    Code:
    /*PROGRAM TO IMPLEMENT DIFFERENT OPERATIONS ON LINKED LISTS
    */
    #include<stdio.h>
    #include<stdlib.h>
    #define draw for(i = 0 ;i<70;++i)printf(" ");
    int flag_error = 0;
    
    struct list
            {
                    int num;
                    struct list *link;
            };
    typedef struct list node;
    
    void display(node **p)
    {
            node **temp = p;
            while(*temp != NULL)
            {
                    printf("%d ->",*temp->num);
    
            } 
    
    }
    main()
    {
             node * head;
            //MENU//
            //while(1)
            {       
                    flag_error = 0;
                    int i;
                    system("clear");
                    draw
                    printf("-----------------------------\n\n\n");
                    draw
                    printf("------LIST MANIPULATION------\n\n");
                    draw
                    printf("      1 : CREATE LIST \n");
                    draw
                    printf("      2 : ADD ELEMENT \n");
     printf("      3 : DELETE ELEMENT \n");
                    draw
                    printf("      4 : DISPLAY LIST \n");
                    draw
                    printf("      5 : EXIT\n");
                    printf("-----------------------------\n\n\n");
                    draw
                    printf("      ENTER YOUR CHOICE\n");
    
    
                    {
                            int choice;
                            scanf("%d",&choice);
                            switch( choice )
                            {
                                    case 1: draw
                                            printf("--------ENTER ELEMENT--------\n");
                                            {
                                                    int num;
                                                    scanf("%d",&num);
                                                    create(&head);
                                            }
                                            printf("--------LIST IS--------\n");
                                            display(&head);
                                            break;
                                    case 2: draw
                                            printf("--------ENTER ELEMENT TO ADD--------\n");
                                            {
                                                    int num;
                                                    scanf("%d",&num);
                                                    draw
                                                    printf("--------1 : ENTER AFTER AN ELEMENT-------- \n");
                                                    draw
                                                    printf("--------2 : ENTER BEFORE AN ELEMENT--------\n");
                                                    draw
                                                    printf("--------3 : ENTER AT A GIVEN POSITION--------\n");
                                                    draw
                                                    printf("----------------\n");
                                                    {
                                                            int ch;
                                                            scanf("%d",&ch);
                                                            draw
                                                            switch(ch)
                                                                    {
                                                                            case 1: 
                                                                            case 2:
                                                                                    printf("--------ENTER THE ELEMENT--------\n");
                                                                                    break;
                                                                            case 3:printf("--------ENTER THE POSITION--------\n");
                                                                    }
                                                            {       
                                                                    int no;
                                                                    scanf("%d",&no);
                                                                    add(&head,num,ch,no);
                                                            }
                                                    }
                                                    draw
                                                    if( flag_error != 1)
                                                    {
    
                                                            printf("--------NUMBER ADDED TO LIST--------\n");
            draw
                                                            printf("--------LIST IS--------");
                                                            display(&head);
                                                    }
                                                    else
                                                    printf("--------LIST IS FULL ...--------");
    
                                            }
                                            break;
                                    case 3 :draw
                                            printf("--------DELETE FROM WHICH POSITION\n--------\n");
                                            {
                                                    int pos;
                                                    scanf("%d",&pos);
                                                    delete(&head,pos);
                                            }
                                            draw
                                            if(flag_error != 1)
                                            {
    
                                                    printf("--------ELEMENT DELETED--------\n");
          draw
                                                    printf("--------LIST IS:");
                                                    display(&head);
                                                    printf("--------");
                                            }
                                            else
                                            printf("--------LIST IS EMPTY--------\n");
                                            break;
                                    case 4 :display( &head );
                                            break;
                                    case 5:return 0 ;
                                    default:draw
                                            printf("--------INVALID CHOICE--------\n");
                            }
                    }
                    draw
                    printf("--------CONTINUE Y?N?--------\n");
                    {
                            char ch ;
                            scanf(" %c",&ch);
                            if( ch != 'y' || ch != 'Y' )
                           return 0;
                    }
            }
    }
    when I compile,the compiler says "error: request for member ‘num’ in something not a structure or union" about the statement " printf("%d ->",*temp->num);"

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quzah brought this up in your previous post. There is a difference between *temp->num and (*temp)->num. The first dereferences temp->num. The second dereferences temp, then finds the num member. You want the second version, (*temp)->num.

    A few other notes:
    1. It's generally bad to put the ending semicolon on a #define for a number of reasons, not the least of which is it makes the code hard to read (by me and my auto-indenting editor).
    2. You should make temp a single pointer, and set it to *p.
    3. You need a temp = temp->next at the end of the loop body to actually traverse the list.
    4. It's int main(void) and you should have it return an int at the bottom (usually 0).
    5. All draw does is print 70 spaces. This is not a good case for using a macro, and it will make your menu look ugly on the standard 80 char display. Drop it all together, or at least make it a function.
    6. You should put a break at the end of your default case, just to be safe.
    7. You probably ought to make each menu and submenu a function, then your case statements will be readable.

    Admittedly, items 5-7 are suggestions, but I think they're worth following (obviously, since I mentioned them).
    Last edited by anduril462; 03-15-2011 at 10:12 PM. Reason: Fixed incorrect item 2

  3. #3
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Thanks for telling me about the de referencing...it worked....Thanks for all suggestions

    Is this correct code
    Code:
    void display(node **p)
    {
            node **temp = p;
            while(*temp != NULL)
            {
                    printf("%d ->",(*temp)->num);
                    temp = &((*temp)->link);
    
            } 
    
    }
    Last edited by linuxlover; 03-15-2011 at 10:31 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, what I initially meant was:
    Code:
    void display(node **p)
    {
            node *temp = p;
            while(temp != NULL)
            {
                    printf("%d ->",(*temp)->num);
                    temp = (*temp)->link;
            } 
    }
    But I'm going to change my suggestion slightly. You aren't modifying *p in the function (i.e. you aren't trying to change the value outside the function), so there is no need to pass a double pointer. You can just do:

    Code:
    void display (node *p)
    {
        while (p != NULL) {
            printf("%d ->", p->num);
            p = p->next;
        }
    }
    That only changes the local copy of p, so you don't have to worry about anything outside that function changing. It also avoids your awkward dereferencing issue and allows you to drop the temp variable.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    In your eg there is no point to pass pointer to pointer.
    Since you are not going to modify what pointer points.

  6. #6
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Quote Originally Posted by anduril462 View Post
    No, what I initially meant was:
    Code:
    void display(node **p)
    {
            node *temp = p;
            while(temp != NULL)
            {
                    printf("%d ->",(*temp)->num);
                    temp = (*temp)->link;
            } 
    }
    But I'm going to change my suggestion slightly. You aren't modifying *p in the function (i.e. you aren't trying to change the value outside the function), so there is no need to pass a double pointer. You can just do:

    Code:
    void display (node *p)
    {
        while (p != NULL) {
            printf("%d ->", p->num);
            p = p->next;
        }
    }
    That only changes the local copy of p, so you don't have to worry about anything outside that function changing. It also avoids your awkward dereferencing issue and allows you to drop the temp variable.
    Ok.....my aim is to clarify my mis concepts about pointer to pointer ...In the other way it can be done in a simple manner...But my doubt is that
    data type of p is node**
    you wrote node *temp =p;
    why it is node *temp;
    data type of temp is node*
    you assigns node** type variable to node* type variable.....please clarify...

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It is typo. Should be node *tmp =*p
    deferencing needs to change too.
    Last edited by Bayint Naung; 03-15-2011 at 11:27 PM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, it was a typo. Thanks Bayint!

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by linuxlover View Post
    Ok.....my aim is to clarify my mis concepts about pointer to pointer ...In the other way it can be done in a simple manner...But my doubt is that
    data type of p is node**
    you wrote node *temp =p;
    why it is node *temp;
    data type of temp is node*
    you assigns node** type variable to node* type variable.....please clarify...
    Frankly I think your biggest misconception is that you seem to think you need them when most often you don't. I've been programming in C for about 6 years now and I'd bet real money you could count the number of places I've used pointer to pointer on the fingers of one hand.

    You're doing way more work than you need to....

    Go back to my original explaination in the other thread...
    after the others have taken you to the same place...
    does it now make sense to you?

  10. #10
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Thanks to common Tater...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

Tags for this Thread