I have numbers in a linked list which i am supposed to convert to char* and return .
Here is is the function where the problem probably exists.
char* int_str(struct Node** head,char* result) //head is pointer to singly linked list
{
struct Node* temp = *head;
char* string1="";
char* str;
while(temp != NULL)
{
string1=myitoa(temp->data,string1); // myitoa() works fine
str=(char*)malloc(1+strlen(string1));
strcpy(str,string1);
strcat(result,str);
temp=temp->next;
}
return result;
}
The last call to temp->data always ends up having unknown garbage values . (The linked list is built correctly as printing the linked list works properly.)
Example : the linked list is 1->2->3
The last call to temp->data in the function gives 50(some garbage value) ie 1->2->50
while in the main function the list correctly gives 1->2->3
The last variable ends up as garbage in this function but shows correctly in main function why ?