2

C - Having problems with the input / output of my structure using 2 functions

 2 years ago
source link: https://www.codesd.com/item/c-having-problems-with-the-input-output-of-my-structure-using-2-functions.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C - Having problems with the input / output of my structure using 2 functions

advertisements

Ok so my objective is to make a structure and give it some values through an input function and output them through an output function, for some reason its output is some scrambled symbols : -1    ░ùI&@0.000000

Any help?

P.S just to note, when I had my input (scanf statements) and output( printf statements) in the function scan_element, it worked just fine. but when I divided it between two functions, it posted the scrambled symbols above.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct element_t  {
        int number;
        char name[20];
        char symbol[3];
        char type[20];
        float atomic_weight;
        char shells[6];
    };

void scan_element(struct element_t pl);
void print_element(struct element_t pl);

int main(int argc, char *argv[])
{
    struct element_t pl;
    scan_element(pl);
    print_element(pl);
    return 0;
}

void scan_element(struct element_t pl)
{
    printf("number:");
    scanf("%d",&pl.number);

    printf("name:");
    scanf("%s",&pl.name);

    printf("symbol:");
    scanf("%s",&pl.symbol);

    printf("class:");
    scanf("%s",&pl.type);

    printf("atomic weight:");
    scanf("%f",&pl.atomic_weight);

    printf("shells use underscore for spaces :");
    scanf("%s",&pl.shells);
}

void print_element(struct element_t pl)
{
    printf("%d",pl.number);
    printf("%s",pl.name);
    printf("%s",pl.symbol);
    printf("%s",pl.type);
    printf("%f",pl.atomic_weight);
    printf("%s",pl.shells);
}


The issue is you are passing structure variable to scan_element(). When you pass by value, it actually creates a copy of pl in main and gives to scan_element(). The parameters you read in that function will be written to the copied structure variable. So pl in main() remain uninitialized.

Solution You need to pass the address of the pl variable in main to scan_element(). You have to modify scan_element() to accept the address as below.

void scan_element(struct element_t *pl)
{
    printf("number: ");
    scanf("%d", &pl->number);

    printf("name: ");
    scanf("%s", pl->name); //Array name itself points to array. So you don't have to write '&'.

    printf("symbol: ");
    scanf("%s", pl->symbol);

    printf("class: ");
    scanf("%s", pl->type);

    printf("atomic weight: ");
    scanf("%f", &pl->atomic_weight);

    printf("shells use underscore for spaces: ");
    scanf("%s", pl->shells);
}

And modify the main() to pass address.

int main(int argc, char *argv[])
{
    struct element_t pl;

    scan_element(&pl);
    print_element(pl);

    return 0;
}

No changes required in print_element().


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK