Here it is the code on C:
here it is the function:
#include <stdio.h>
#include <stdlib.h>
struct worker
{
char firstName;
char lastName;
char middleName;
int age;
float height;
};
void enterData();
void displayData();
void makeCalc();
struct worker *b;
int count;
int main(void)
{
enterData();
displayData();
makeCalc();
free(b);
return 0;
}
void enterData()
{
char firstName;
char lastName;
char middleName;
int age;
float height;
printf("Количество работников: \n");
scanf("%d", &count);
if(count <= 0){
exit(0);
}
b = (struct worker *)malloc(count*sizeof(struct worker));
for(int i = 0; i<count; i++){
printf("Введите имя, фамилию, отчестве, возраст и рост через пробел: \n");
scanf("%s%s%s%d%f", &firstName, &lastName, &middleName, &age, &height);
struct worker a = (struct worker) {firstName, lastName, middleName, age, height};
b[i] = a;
}
}
While runtime in this part if program is showed Segmentation fault (core dumped):
for(int i = 0; i<count; i++){
printf("Enter name, surname and middle name: \n");
scanf("%s%s%s%d%f", &firstName, &lastName, &middleName, &age, &height);
struct worker a = (struct worker) {firstName, lastName, middleName, age, height};
b[i] = a;
What could be the reason of this? I am new to C;
P.S I am using GCC compiler on ubuntu 12.04. and common text editor.
The problem is that you are using char type in declaration and using
%sin scanfThese have to be changed to hold strings
should be changed to