hi,
I have a linked list of struct that contains string and int.
(name and price) the list is sorted by price (ascending order)
i built a function that returns the name of a product by its place
in the list.
Code:
char* get_name(pData data, int place)
{
int i;
char* name;
Node* temp=data->tail;
for (i=1; i<place;i++)
{
if (temp->prev==NULL) return NULL;
temp=temp->prev;
}
name=(char*)malloc(sizeof(char*)*strlen(temp->name));
strcpy(name,temp->name);
return name;
}
the function returns the correct pointer but after the malloc,
the name of the last (data->tail) product in the list changes i.e.
before the malloc: data->tail->name="A string with 24 chars"
and after the malloc: data->tail->name="A string with 24 chars!" (or A instead of !)
whats wrong with the code?
thanks