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 ).
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.
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
Atlast got the intended results
Thanks guys,
moral of the story : Simple mistakes leaves people in a big disarray.
diff between (*tlist)-> next and (**tlist).next ?
can any body explain me this please ..?
looking forward for your responses.
thanks