連結された文字列の合計サイズが特定のバイト数に等しくなくなるまで、連結リストのノード内のすべての文字列を連結した文字列を返すプログラムを作成していました。しかし、ループ変数の値は最後の繰り返しでガベージになります。誰でも理由を教えてもらえますか?
typedef struct li {
char *data;
int len; //structure for the linked list
struct li *next;
} 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;
}
}