I have a stuct in C, and I use a function to get the values for the structure from the user. Below is my structure.
typedef struct {
char *name;
char *chemical_symbol;
char *class;
int atomic_number;
double atomic_weight;
int *electrons;
} element_t;
This is the function I am using to get the values from the user. The problem lies in this function after asking for the chemical symbol. I get Bus Error: 10. My understanding of a bus error is when the processor cant attempt the memory access. Any help is appreciated. Thank you!
element_t scan_element() {
element_t element;
printf ("Enter New Element Information:\n\n");
printf("Element Name: ");
scanf("%s", element.name);
printf("Element Chemical Symbol: ");
scanf("%s", element.chemical_symbol);
printf("Element Class: ");
scanf("%s", element.class);
printf("Element Atomic Number: ");
scanf("%d", &element.atomic_number);
printf("Element Atomic Weight: ");
scanf("%lf", &element.atmoic_weight);
printf("Element Electrons: ");
scanf("%p", &element.electrons);
return(element);
}
You have to allocate memory for the object pointed by
element.namefor example by usingmalloc. Without proper allocationelement.nameis an invalid pointer.