loop variable shows garbage value
I was writing a program to give back a string having the concatenation of
all the strings in the nodes of a linked list till the total size of the
concatenated string is not equal to a a particular number of bytes. But
the value of the loop variable becomes garbage in the last iteration. Can
anyone tell me why?
struct li{
char *data;
int len; //structure for the linked list
struct li *next;
};
typedef struct li node;
node *head;
void create()
{
int i,n;
char num[100]; //the value of i when equal to n-1
becomes garbage
//of the proper n-1
node *temp;
printf("enter the number of nodes\n");
scanf("%d",&n); //supposed to create the list
printf("n=%d",n);
printf("enter the strings to be kept in the nodes\n");
for(i=0;i<n;i++)
{
if(i==0)
{
head=(node*)malloc(sizeof(node));
temp=head;
}
else{
temp->next=(node*)malloc(sizeof(node));
temp=temp->next;
}
scanf("%s",num);
temp->data=num;
temp->len=strlen(num);
//in the final loop shows error as value of i becomes garbage
}
temp->next=NULL;
}
void g (int n)
{
int t=0,m; //the main logic
char *f={'\0'};
node *temp;
temp=head;
while(temp!=NULL && t!=n)
{
m=sizeof(temp->data);
strcat(f,temp->data);
t+=m;
temp=temp->next;
}
}
No comments:
Post a Comment