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
#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