How to create structure vector within function

Asked

Viewed 239 times

3

Good morning! I got recovery again in the matter of algorithms, what got me most was structure and passing parameters. I decided to create an activity to train for recovery Monday.

Following the teacher’s slides I finally did, the code is working, which doesn’t explain is... How to run with for, I have a data structure and pass it to a function using pointers.

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

typedef struct mercearia
{
    char mercadoria[20];
    int quantidade;
    int valor;

} mercearia;


void EstruturaCadastro(struct mercearia *x)
{
    printf("\n");

    printf("Insira nome do produto: ");
    scanf("%s", &(*x).mercadoria);setbuf(NULL, stdin);

    printf("Insira a quantidade do produto: ");
    scanf("%i", &(*x).quantidade);setbuf(NULL, stdin);

    printf("Insira o valor do produto: R$ ");
    scanf("%i", &(*x).valor);setbuf(NULL, stdin);
}

void ExibirCadastroProdutos(struct mercearia x)
{
    printf("\n");

    printf("\nO produto cadastrado %s", x.mercadoria);
    printf("\nValor quantidade do produto %i", x.quantidade);
    printf("\nValor da unidade do produto %i", x.valor);

}

    int main(void)
    {
        setlocale(LC_ALL, "");
        struct mercearia produto;

        EstruturaCadastro(&produto);
        ExibirCadastroProdutos(produto);
    }

I figured it would work out this way:

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

typedef struct mercearia
{
    char mercadoria[20];
    int quantidade;
    int valor;

} mercearia;


void EstruturaCadastro(struct mercearia *x)
{
    int i;
    printf("\n");

    for(i = 0; i < 5; i++)
    {
        printf("Insira nome do produto: ");
        scanf("%s", &(*x)[i].mercadoria);setbuf(NULL, stdin);

        printf("Insira a quantidade do produto: ");
        scanf("%i", &(*x)[i].quantidade);setbuf(NULL, stdin);

        printf("Insira o valor do produto: R$ ");
        scanf("%i", &(*x)[i].valor);setbuf(NULL, stdin);
    }
}

void ExibirCadastroProdutos(struct mercearia x)
{
    int i;
    printf("\n");

    for(i = 0; i < 5; i++)
    {
        printf("\nO produto cadastrado %s", x.mercadoria);
        printf("\nValor quantidade do produto %i", x.quantidade);
        printf("\nValor da unidade do produto %i", x.valor);
    }
}

    int main(void)
    {
        setlocale(LC_ALL, "");
        struct mercearia produto[5];

        EstruturaCadastro(&produto);
        ExibirCadastroProdutos(produto);
    }

But then it returns to me 300 thousand errors... Someone could point out where I’m going wrong?

Errors submitted by the IDE:

||=== Build file: "no target" in "no project" (Compiler: Unknown) ===| C: Users lelre Documents 1 - Personal Faculty ALG List of activities Test archive. c||In Function 'Structurecadastre':|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|22|error: subscripted value is neither array nor Pointer nor vector|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|22|Warning: Passing argument 2 of 'setbuf' from incompatible Pointer type|

C: Program Files (x86) Codeblocks Mingw include stdio. h|193|note: expected 'char *' but argument is of type 'struct FILE *'|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|25|error: subscripted value is neither array nor Pointer nor vector|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|25|Warning: Passing argument 2 of 'setbuf' from incompatible Pointer type|

C: Program Files (x86) Codeblocks Mingw include stdio. h|193|note: expected 'char *' but argument is of type 'struct FILE *'|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|28|error: subscripted value is neither array nor Pointer nor vector|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|28|Warning: Passing argument 2 of 'setbuf' from incompatible Pointer type|

C: Program Files (x86) Codeblocks Mingw include stdio. h|193|note: expected 'char *' but argument is of type 'struct FILE *'|

C: Users lelre Documents 1 - Personal Faculty ALG List of activities Test archive. c||In Function 'main':|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|50|Warning: Passing argument 1 of 'Structurecadastre' from incompatible Pointer type|

C: Users lelre Documents 1 - Personal Faculty ALG List of activities Test archive. c|14|note: expected 'struct mercearia ' but argument is of type 'struct mercearia ()[5]'|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|51|error: incompatible type for argument 1 of 'Displaysroducts'|

C: Users lelre Documents 1 - Personal ALG College List of activities Test archive. c|32|note: expected 'struct mercearia' but argument is of type 'struct mercearia *'| ||=== Build failed: 4 error(s), 4 Warning(s) (0 minute(s), 0 Second(s)) ===|

  • Good morning Lelre! If possible, please put the errors presented along with your question, to facilitate the analysis of the problem.

  • Good morning boss! I added the bugs presented by the IDE.

1 answer

1


Considering the chunk of code that uses a vector of structures, I have the following observations.

typedef struct mercearia { ... } mercearia;

After that definition of type, mercearia can be used as an abbreviation for the type struct mercearia, making the code much cleaner and clear. So

struct mercearia produto[5];

can be rewritten as

mercearia produto[5];

Another point is the scanf. scanf requires you to pass the address of the memory space you want to store certain information. The operator & gives access to such an address.

Therefore, its function EstruturaCadastro needs to be modified.

void EstruturaCadastro(mercearia *x)
{
    scanf("%s", x[i].mercadoria); // para array de char não é necessário
    scanf(" %i", &(x[i].quantidade)); // espaço em branco consome \n do scanf anterior
    scanf(" %i", &(x[i].valor));
}

Its function ExibirCadastroProdutos needs to receive a pointer to mercearia to be able to traverse the array that is passed as parameter

void ExibirCadastroProdutos(mercearia *x)

There are some other minor adjustments that need to be made. See the code working here.

  • Thank you very much, the code is working 100%!

  • @Lelreferreira consider marking the answer as accepted.

Browser other questions tagged

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