# Scope of Pointer Variables

• 03-24-2008
vlrk
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 ).
• 03-24-2008
Elysia
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*.
• 03-24-2008
vlrk
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.
• 03-24-2008
Elysia
Define it as a variable in main and pass to the function via a pointer.
• 03-25-2008
vlrk
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
• 03-25-2008
matsp
You need parentheis:
Code:

`(*tlist)->next`
--
Mats
• 03-25-2008
vart
(*tlist)->next
• 03-25-2008
Elysia
Or you can do (**tlist).next.
• 03-25-2008
vlrk
Atlast got the intended results
Thanks guys,

moral of the story : Simple mistakes leaves people in a big disarray.
• 03-25-2008
vlrk
diff between (*tlist)-> next and (**tlist).next ?
can any body explain me this please ..?

thanks
• 03-25-2008
laserlight
(*tlist)->next is the equivalent of (*(*tlist)).next which is equivalent to (**tlist).next
• 03-25-2008
matsp
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
• 03-25-2008
Elysia
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.