I am having trouble with structure definitions. I understand the syntax but can’t use it correctly to make a working program. My assignment is to define a structure type element_t to represent one of the elements of the periodic table. Components should include atomic number, name, symbol, class, weight and a seven element array of integers for the number of electrons in each shell. The following are the components of an element_t structure for sodium.
11 Sodium Na alkali_metal 22.9898 2 8 1 0 0 0 0
Define and test I/O functions scan_element and print_element.
Here is my code… but no worky. I dont understand why the compiler says I haven’t initialized element_t. Thanks in advance.
***Ended up with the following code, also added the appropriate loops and & signs although it is not shown below and everything worked fine. I was curious to know how the powerpoint slides in my class used {2 8 1 0 0 0 0} to populate the int array instead of a loop. The book I am using uses the loop method as well but I wanted to try the {} way of doing it. Thanks again.
#include <stdio.h>
#include <string.h>
typedef struct {
int atomic_num;
char* element_name; revised: char element_name[25];
char* element_symbol; revised: element_symbol[2];
char* element_class; revised: char element_class[25];
double atomic_weight;
int electrons[7];
} element_t;
int scan_element(); revised: element_t scan_element();
void print_element(element_t my_element);
int scan_element() {
element_t my_element;
printf("Enter Atomic Number> ");
scanf("%d", my_element.atomic_num);
printf("Enter Element Name> ");
scanf("%s", my_element.element_name);
printf("Enter Element Symbol> ");
scanf("%s", my_element.element_symbol);
printf("Enter Element Class> ");
scanf("%s", my_element.element_class);
printf("Enter Atomic Weight> ");
scanf("%lf", my_element.atomic_weight);
printf("Enter Electons in each shell> ");
scanf("%d", my_element.electrons);
print_element(my_element);
return 0;
}
void print_element(element_t my_element) {
printf("%d %s %s %s %lf %d \n",my_element.atomic_num,my_element.element_name, my_element.element_symbol,my_element.element_class,my_element.atomic_weight,&my_element.electrons);
}
int main()
{
scan_element(); revised: print_element(scan_element());
return 0;
}
I’ve identified some problems in your code:
1)When reading with scanf non-pointer types(like int or float), use the & operator. Scanf needs a pointer to your variable, not the variable itself.
2)If you declare your strings as dynamic (char*), be sure to use malloc to allocate memory for them. Otherwise declare them statically like char my_string[30].
3)Your scan_element function should return an element_t structure, not an int. Alternatively you could pass a pointer to an existing element_t structure and modify it in your function.
4)You cannot read/print a int array directly using scanf/printf. Every element must be read separately.
5)Your scan_element function shouldn’t probably contain print_element 😛
If you want to learn more about Console I/O in ANSI C, perhaps this article could be useful.
Here is the corrected code: