Error passing a structure by reference in C [closed]

0

I'm learning C. However, I had a problem passing a structure to a function. I can not figure out what the error is.

I will be grateful if anyone helps me. Me Sorry for the bad documentation. However, the code is very simple.

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

#define QT_EMP 2

/*Address*/
struct address{
    char country[20];
    char city[20];
    char st[20];
    unsigned long int zip;
};

/*Personal Data*/
struct pd{
    char name[20];
    int age;
    float size;
    float heavy;
};

/*Employes*/
struct emp{
    unsigned freedom: 1;
    unsigned is_emp : 1;
    struct pd data;
    struct address addr;
};

int menu ();
void del (struct emp *p, int i);
void init (struct emp *p);
int check (struct emp *p);
void show (struct emp *p);
void enter (struct emp *p, int i);

/*---------------------*/
/*--------MAIN---------*/
int main() {
    struct emp actors[QT_EMP];
    int choice = 0;
    init(&actors);

    while (choice < 1 || choice > 4) {
        choice = menu();
        switch (choice) {
            /*Insert*/
            case 1:
            {
                int i;
                i = check(&actors);
                if (i != -1) enter(&actors, i);
                else printf("\nBad entry!");
                break;
            }

            /*Delete*/
            case 2:
            {
                del();              
                break;
            }

            /*Show*/
            case 3:
                show(&actors);
                break;

            /*Exit*/
            case 4:
                exit (1);
        }
    }

    return 0;
}
/*---------------------*/
/*---------------------*/

/*Delete*/
void del (struct emp *p, int i) {
    int i;
    printf("Type the indice: ");
    scanf("%d", &i);
    p[i]->is_emp = 0;
}

/*Initiate*/
void init (struct emp *p) {
    register int i;
    for (i = 0; i < QT_EMP; ++i) 
        p[i]->freedom = 1;
}
/*Check for empty indice*/
int check (struct emp *p) {
    register int i;
    for (i = 0; i < QT_EMP; ++i) {
        if (p[i]->freedom) return i;
    }
    return -1;
}

/*Menu*/
int menu () {
    int choice = 0;
    while (choice < 1 || choice > 4) {
        printf("------MENU------");
        printf("Select an option:\n");
        printf("1) Insert;\n");
        printf("2) Delete;\n");
        printf("3) Show;\n");
        printf("4) Exit. ");
        scanf("%d", &choice);
    }
    return choice;
}

/*Show*/
void show (struct emp *p) {
    register int i;
    for (i = 0; i < QT_EMP; ++i)
        if (p[i]->freedom) {
            if (p[i]->is_emp) printf("\nEmployee");
            else printf("\nEx Employee!");
            printf("\n----Personal Data----");
            printf("\nName: %s", p[i]->data.name);
            printf("\nAge: %d", p[i]->data.age);
            printf("\nSize: %3f", p[i]->data.size);
            printf("\nHeavy: %f", p[i]->data.heavy);
            printf("\n----Adress----");
            printf("\nCountry: %s", p[i]->addr.country);
            printf("\nCity: %s", p[i]->addr.city);
            printf("\nStreet: %s", p[i]->addr.st);
            printf("\nZIP code: %d", p[i]->addr.zip);
        }
}

/*Enter the data of emp*/
void enter (struct emp *p, int i) {
    p->freedom = 0;
    p->is_emp = 1;
    /*Personal data*/
    printf("\n----Personal Data----");
    printf("\nName:");
    scanf("%20s", &p[i]->data.name);
    printf("\nAge: ");
    scanf("%d", &p[i]->data.age);
    printf("\nSize: ");
    scanf("%f", &p[i]->data.size);
    printf("\nHeavy: ");
    scanf("%f", &p[i]->data.heavy);
    /*Address*/
    printf("\n----Adress----");
    printf("\nCountry: ");
    scanf("%20s", &p[i]->addr.country);
    printf("\nCity: ");
    scanf("%20s", &p[i]->addr.city);
    printf("\nStreet: ");
    scanf("%20s", &p[i]->addr.st);
    printf("\nCity: ");
    scanf("%d", &p[i]->addr.zip);
}
    
asked by anonymous 26.01.2017 / 07:45

1 answer

0

Your code has a lot of compilation errors and a few warnings .

Let's break it down. First, change these lines:

init(&actors);
i = check(&actors);
if (i != -1) enter(&actors, i);
show(&actors);

For these:

init(actors);
i = check(actors);
if (i != -1) enter(actors, i);
show(actors);

That is, do not use &actors within main , just use actors . The reason for this is that arrays can be passed to functions as pointers, and their functions already expect pointers.

Then, in its main function, there is this:

del();

Only del has two parameters.

Incidentally, look at your del function:

void del (struct emp *p, int i) {
    int i;
    printf("Type the indice: ");
    scanf("%d", &i);
    p[i]->is_emp = 0;
}

There are two variables i . A of the parameter and the local variable. I think what you wanted was this:

void del (struct emp *p) {
    int i;
    printf("Type the indice: ");
    scanf("%d", &i);
    p[i]->is_emp = 0;
}

And no main :

del(actors);

And also correct the function prototype.

Finally, all places where there is p[i]-> should be p[i]. There are 23 places of these, including these two (you forgot [i] ):

p->freedom = 0;
p->is_emp = 1;

And in scanf , do not use & when reading a string. For example:

scanf("%20s", p[i].data.name);

And also, when using scanf or printf in zip , use "%lu" instead of "%d" .

Finally, in main , instead of using exit (1); , you'd prefer to put return 0; in place. I also think that in main , its while (choice < 1 || choice > 4) was to be while (1) , and in this case return 0; that is in the end is unnecessary.

    
26.01.2017 / 13:57