Witam.
Pracuję nad listą jednokierunkową. Mam spory problem aby zrozumieć jak przekazać łańcuch znaków do elementu listy. Zapisując listę, za pomocą wskaźnika, losowo generowanymi danymi okazuje się, że wszystkie dane mają wartość ostatniego wylosowanego elementu. Przy przesyłaniu "gotowego" łańcucha nie ma takiego problemu.Czy ktoś wie dlaczego tak się dziej i jak to poprawić.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node {
int a;
char *b;
struct node * next;
} list;
list* createList(){
list * head = NULL;
head = malloc(sizeof(list));
return NULL;
}
list *insert(list **head, int _a, char *_b){
list * temp;
temp = malloc(sizeof(list));
temp->a = _a;
temp->b = _b;
temp->next = *head;
*head = temp;
}
void showList(list * head){
list * temp = head;
if(temp == NULL)
printf("Empty \n");
while (temp != NULL) {
printf("%d %s",temp->a, temp->b);
printf("\n--------------------------------------------------------\n");
temp = temp->next;
}
}
int main (){
time_t czas,start=time(NULL);
time(&czas);
srand(time(&czas));
int a=0,cnt=0;
char b[4];
list *list;
list = createList();
while(cnt<10){
a=( rand()%70+50);
for(int i=0;i<3;i++)
b[i]=(rand()%26+65);
cnt++;
insert(&list,a,b);
printf("b = %s \n",b);//wyswietla dobrze
}
printf("Podaj 3 znaki \n");
scanf("%s",b);
insert(&list,1,b);
showList(list);//zamienia szystko na aktualne b
printf("Podaj 3 znaki \n");
scanf("%s",b);
insert(&list,2,b);
insert(&list,3,"dziala");//w ten sposob dziala
showList(list);// ostatnia wartosc b jest przypisana do wszystkich elementow listy
return 0;
}