Thread: Scope of Pointer Variables

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Scope of Pointer Variables

    I have written a piece of code to add nodes to the linked list .

    Code:
    #include <stdio.h>
    
    
    struct node
    {
      int n;
      struct node *next;
    }typedef node_t;
    
    node_t *top = NULL;
    
    int main()
    {
      node_t *list = NULL;
      int choice;
    
      while(1)
        {
          printf("Please Enter your choice \n");
          printf("1.Addition \n");
          printf("2.Exit \n");
          scanf("%d",&choxcice);
    
          switch(choice)
            {
            case 1:
              addition(&list);
              break;
            case 2:
              return(0);
              break;
            }
        }
    
      return 0;
    }
    
    int addition(node_t *list)
    {
      int number;
      node_t *newnode;
    
      printf("Take number \n");
      scanf("%d",&number);
    
      newnode = malloc(sizeof(node_t) * 1);
    
      if (top == NULL)
      {
         newnode->n = number;
         newnode->next = NULL;
         top = newnode;
         list = newnode;
      }
      else
      {
        newnode->n = number;
        newnode->next = NULL;
        list->next = newnode;
        list = list->next;
      }
      return 0;
    }



    Here my doubts are.

    1) If i give "list" argument without & in addtion function , its showing some other behaviour where i cannot add to it, i think the pointer variable means itself address so why do again i need send address here?

    2) after one iteriation , the list is becoming null , ( address it s showing in gdb after one node addition is NULL ).

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For 1) You have a problem.
    A pointer is just a variable that holds an address. So the variable is in itself, passed by value. So you are trying to modify a variable passed by value in the function. Thus, the main function will never see the changes in list.
    To fix that, you need to pass a pointer to the variable you want to change. The variable's type is node_t*, so a pointer would become node_t**.
    And heed compiler warnings! You should get a warning that you're trying to convert node_t** to node_t*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    How to pass pointer variable as a function argument?

    Here my interntion is i donot want to keep another global variable.

    I want to maintian *list variable of type node_t which point to current node.

    Can any body suggest how can i achive this ?

    if i simple put a global variable liek top which i use now , it will complete my work but i need

    this as a function argument.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Define it as a variable in main and pass to the function via a pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Still confusion with pointers as arguments

    consider this piece of code

    Code:
    int addition(node_t **tlist)
    {
      int number;
      node_t *newnode;
    
      printf("Take number \n");
      scanf("%d",&number);
    
      newnode = malloc(sizeof(node_t) * 1);
    
     if (top == NULL)
      {
         newnode->n = number;
         newnode->next = NULL;  
         top = newnode; 
         *tlist = newnode;
      }
      else
      {
        newnode->n = number;
        newnode->next = NULL;
       *tlist->next = newnode; /* line number 77 */
        *tlist = *tlist->next; /** line number 78 */
      }
      return 0;
    }




    my understanding is like below

    once i declare a pointer varaible like node_t *list

    then consider list is a variable which is having its own address as 1020 and it is eligible to contains address of the variable of type node_t .

    next step iam passing the address of list to the function i.e noting but 1020 will be passed to addition function .

    next step now when iam adding the first node its fine i.e noting but list = newnode now lets consider newnode address is 1030 . so list is containing 1030 address

    next step now when i add another node , newnode is at address of 1040
    the interntion here is list->next i.e in 1030 memory location next should contain 1040

    so what iam doing here is *tlist->next = 1040 and move the *tlist to here so it can be at the latest node so *tlist = *tlist->next

    1) i donot know where my logic is lagging it s not even compiling . please give your thoughts on my interpretation.

    i get error like

    inkedlist.c:77: error: request for member `next' in something not a structure or union
    linkedlist.c:78: error: request for member `next' in something not a structure or union


    thanks

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need parentheis:
    Code:
    (*tlist)->next
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    (*tlist)->next
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or you can do (**tlist).next.
    Which suits your fancy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Atlast got the intended results

    Thanks guys,

    moral of the story : Simple mistakes leaves people in a big disarray.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    diff between (*tlist)-> next and (**tlist).next ?

    can any body explain me this please ..?

    looking forward for your responses.


    thanks

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    (*tlist)->next is the equivalent of (*(*tlist)).next which is equivalent to (**tlist).next
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    a->b is short form of (*a).b. Since you have (*a)->b, we can rewrite that as (*(*a)).b, and remove the now unnecessary parenthesis: (**a).b.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Further, when you do *a->b, the compiler evaluates the "a->b" part first, and then the "*a" part. So you try to dereference a pointer to pointer type**, so it becomes type*, which doesn't have a "next" member. Therefore, you must tell the compiler to dereference a first, and then access the member.
    This is done by using (*a) to prioritize it, and then a->b to access b.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Global scope and passing pointer
    By Dave++ in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2007, 03:52 PM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer to variables in Doc.h is there a way of doing it?
    By drb2k2 in forum Windows Programming
    Replies: 0
    Last Post: 04-14-2003, 07:41 AM