Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:18:21+00:00 2026-06-14T14:18:21+00:00

I am having trouble with structure definitions. I understand the syntax but can’t use

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:18:22+00:00Added an answer on June 14, 2026 at 2:18 pm

    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:

    include <stdio.h>
    #include <string.h>
    
    typedef struct  {
        int atomic_num;
        char element_name[30];
        char element_symbol[3];
        char element_class[30];
        double atomic_weight;
        int electrons[7];
    } element_t;
    
    element_t scan_element();
    void print_element(element_t my_element);
    
    element_t scan_element() {
        int i = 0;
        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);
    
        for(i = 0; i<7; i++)
        {
            printf("Enter Electon - electron nr %d\n",i);
            scanf("%d", &my_element.electrons[i]);
        }
    
        return my_element;
    }
    
    void print_element(element_t my_element) {
        int i;
        printf("%d %s %s %s %lf\n",
               my_element.atomic_num,
               my_element.element_name, 
               my_element.element_symbol,
               my_element.element_class,
               my_element.atomic_weight);
        for(i = 0; i<7; i++)
        {
            printf("%d",my_element.electrons[i]);
        }
        puts("\n");
    }
    
    int main()
    {
        element_t val = scan_element();
        print_element(val);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having trouble working grabbing a dynamically created image from this service: http://opsin.ch.cam.ac.uk/opsin/USER-STRUCTURE-TO-GENERATE.png Im
I'm having trouble modeling a particular database structure I'm working on. To be short,
I am having trouble in accessing an array within a structure. How can I
I am trying to correctly define a tree structure in GORM and having trouble.
I know i want to use an update statement but im having trouble with
I'm having trouble understanding how any data structure can be nonblocking. Say you're making
I am having trouble understanding how I should structure my database tables. I want
I'm having trouble adding Facebook Connect to my iPhone Project. Here's the directory structure:
I am having some trouble parsing xform xml with javascript. The structure of the
I'm having a little trouble as I decide how to structure my projects. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.