Error when passing a structure by reference in C

Asked

Viewed 60 times

0

I’m learning C. However, I had a problem getting through a structure to a function. I cannot understand which is the error.

I will be grateful if someone 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);
}
  • 3

    what is going wrong?

  • 1

    Try to cut the code to have only the part that has the problem, as well as the error that accompanies the problem

1 answer

0


Your code has a lot of build errors and a few more warnings.

Let’s take parts. 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);

I mean, don’t use &actors within the main, just use actors. The reason is that arrays can be passed to functions such as pointers, and their functions already await pointers.

Then, in your job main, there’s that:

del();

Only that del has two parameters.

By the way, look at your function del:

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. 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 in the main:

del(actors);

And also fix the function prototype.

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

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

And in the scanf, don’t use the & when reading a string. For example:

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

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

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

  • Well, lack of attention even. I passed the pointer of a list to a function, since it was only possible to do it this way. The book, which I am using as a reference is excellent, but it is very old, from 1987. Thanks for the help, I’m glad you’re willing to help me instead of criticizing the post. I did not present the errors, since they were in large quantities, as you may notice!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.