#include <stdio.h>
#include <stdlib.h>
#define VEL 2
typedef struct
{
char ime[15];
char prezime[15];
int dob;
int staz;
} employee;
void upis(employee *);
void ispis(employee *);
int main(int argc, char **argv)
{
employee radnik[VEL];
upis(&radnik[VEL]);
ispis(&radnik[VEL]);
return 0;
}
void upis(employee * r)
{
int i;
printf("Upis podataka:\n==============\n");
for (i = 0; i < VEL; i++)
{
printf("Upisite ime i prezime %d. radnika:\n", i + 1);
scanf(" %15[^\n]", (r + i * sizeof(employee))->ime);
scanf(" %15[^\n]", (r + i * sizeof(employee))->prezime);
printf("Upisite dob i staz %d. radnika:\n", i + 1);
scanf("%d", &(r + i * sizeof(employee))->dob);
scanf("%d", &(r + i * sizeof(employee))->staz);
}
}
void ispis(employee * r)
{
int i;
for (i = 0; i < VEL; i++)
{
printf("Ime:%s\nPrezime:%s\n", (r + i * sizeof(employee))->ime,
(r + i * sizeof(employee))->prezime);
printf("Dob:%d\nStaz:%d\n\n", (r + i * sizeof(employee))->dob,
(r + i * sizeof(employee))->staz);
}
}
The code actually works but in the end always returns segmentation fault. I'm guessing I did something wrong with pointers and addressing struct elements. Please help and thanks in advance!!
EDIT: Thanks everyone each answer was helpful!