我正在编写一个程序来返回一个字符串,该字符串具有链表节点中所有字符串的串联,直到串联字符串的总大小不等于特定的字节数。但是循环变量的值在最后一次迭代中变成了垃圾。谁能告诉我为什么?
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;
}
}