Little Confused

M

manochavishal

#include<stdio.h>
#include<stdlib.h>

typedef struct binary* binaryptr;
typedef struct binary
{
int data;
binaryptr next;

}Binary;

void AddNum(binaryptr , int);

int main(int argc, char **argv)
{
int input,num;
binaryptr list;
list = NULL;
if(argc!=2)
{
printf("Arguments not enough\n");
exit(EXIT_FAILURE);

}
input = atoi(argv[1]);

while(input!=0)
{
num= input%2;
AddNum(list,num);
input /= 2;
}
while(list!=NULL)
{
printf("%d\n",list->data);
list = list->next;
}

return 0;


}

void AddNum(binaryptr list, int num)
{
binaryptr temp,NewNode;
printf("IN Add Num\n");
if(list == NULL)
{
NewNode = malloc(sizeof(Binary));
printf("IN List Null\n");
NewNode->data = num;
NewNode->next = NULL;

list = NewNode;

}
else
{
temp = list;
while(temp->next!=NULL)
{
temp = temp->next;
}
NewNode = malloc(sizeof(binaryptr));
NewNode->data = num;
NewNode->next = NULL;
temp->next = NewNode;

}

}

Why would i will not get the List through this code.
Each time i am entering if(list == NULL){
} Loop.

Is the reason is that i am allocating memory in the fucntion and when i
will return from that function that object may cease to exist.
I am passing a pointer to structure. So if i allocate some memory to it
in some function it should not sho itself as NULL.

Also if i pass pointer to a pointer to structure it works fine.

Why???

Thanx in advance
Vishal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Little Confused 6
a simple struct code failing 12
linked list 4
linked list implementation 8
Double linked list 9
C pipe 1
Variable-sized lines of text in linked list 47
Infinite loop problem 1

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top