Witajcie. Otóż mam za zadanie napisać bazę danych, która będzie wczytywać i zapisywać dane do pliku csv. Jednak pierwszy problem pojawił się przy wyświetlaniu listy.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct lista {
int val;
struct lista *next;
};
void pokaz_baze(struct lista *element) {
if(element==NULL) {
printf("\nBaza danych jest pusta!\n");
}else{
while(element!=NULL) {
printf("%d \n", element->val);
element = element->next;
}
}
}
void dodaj_rekord(struct lista *element) {
struct lista *nowy = (struct lista*)malloc(sizeof(struct lista));
printf("Podaj numer indeksu: ");
scanf("%d", &(nowy->val));
nowy->next = NULL;
if((element)==NULL)
{
element = nowy;
}
else{
struct lista *osoba = element;
while(osoba->next!=NULL) {
osoba = osoba->next;
}
osoba -> next = nowy;
}
}
int main()
{
struct lista *head = (struct lista*)malloc(sizeof(struct lista));
int wybor = 0;
do{
printf("\n1. Wyswietl baze\n");
printf("2. Dodaj rekord\n");
printf("3. Modyfikuj rekord\n");
printf("4. Usun rekord\n");
printf("5. Zakoncz program\n");
printf("Twoj wybor: ");
scanf("%d", &wybor);
switch(wybor) {
case 1:
pokaz_baze(head);
break;
case 2:
dodaj_rekord(head);
break;
case 3:
break;
case 4:
break;
case 5:
printf("\nKonczenie programu!\n");
break;
default:
printf("\nZly wybor. Sprobuj ponownie.\n");
break;
}
}while(wybor!=5);
return 0;
}
Już pierwszy element jest 0 (podejrzewam, że to przez malloc) ale inne próby kombinowania np. struct lista *head = NULL; pokazywały mi SEGMENTATION FAULT. Nie mogę doszukać się błędu. Domyślam się, że jest śmieszny, ale go nie widzę i trochę się gubię.