Print and receive Vector Struct values in a function

Asked

Viewed 182 times

0

I looked in several places about it but I couldn’t find any correct answer, I wanted to know the correct method of filling a structure vector passed by reference to one function and then printing in another, in this code below I tried to do something of the kind to try to fill but it did not work, thanks since.

#include <stdio.h>

struct carnaval
{
        char nome [10];
        char cor [10];
        int quantidade;
};


int main ()
{
        int x=1;

        struct carnaval blocos[2];
        void listar();
        void incluir();
        void buscar();

        while (x!=0)
        {

                printf ("1) Incluir\n");
                printf ("2) Listar\n");
                printf ("3) Buscar\n");
                printf ("0) Sair\n");

                scanf ("%d", &x);

                if (x==0)
                {
                        break;
                }
                switch (x)
                {
                        case 1:
                        incluir (blocos);
                        break;
                        case 2:
                        listar (blocos);
                        break;
                        case 3:
                        buscar ();
                        break;
                        case 0:
                        break;
                        default:
                        printf ("valor inválido\n");
                        break;
                }
        }
}

void incluir (struct carnaval *incluido[])
{
        int i;
        for (i=0; i<2; i++)
        {
                getchar();
                fgets ((*incluido)[i].nome, 10, stdin);
                fgets ((*incluido)[i].cor, 10, stdin);
                scanf ("%d", &(*incluido)[i].quantidade);
        }

}

void listar (struct carnaval print[])
{
        int y;

        for (y=0; y<2; y++)
        {
                printf ("\n%s", print[y].nome);
                printf ("%s", print[y].cor);
                printf ("%d\n\n", print[y].quantidade);
        }
}

void buscar ()
{
        printf ("teste\n");
}

1 answer

0


Your code has some errors. The first one is that when you want to pass an array as a function parameter, the syntax of the type void incluir (struct carnaval *incluido[]) can be used when working with a two-dimensional vector.

On the other hand, when working with one-dimensional vectors, which is your case, you must declare as a parameter or a vector (one-dimensional), e.g. struct carnaval incluido[], or a pointer, e.g. struct carnaval *incluido.

The way you access data from incluido is also mistaken. For example, in place of (*incluido)[i].nome, you should have written only incluido[i].nome. In this regard, remember that vectors are already passed by reference and that [i] is enough to access the contents of the array at the position i.

That said, it follows the function void incluir() with the corrections of the errors pointed:

void incluir (struct carnaval incluido[])
{
        int i;
        for (i=0; i<2; i++)
        {
               getchar();
               fgets (incluido[i].nome, 10, stdin);
               fgets (incluido[i].cor, 10, stdin);
               scanf ("%d", &incluido[i].quantidade);
        }

}
  • It really worked, thank you very much for the answer and for the help!

  • @Marlonviníciusbuosi if my answer helped solve your problem, you should accept it to signal to the community that your problem has been solved. More details, cf: https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-reply.

Browser other questions tagged

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