How to create structure vector within function

2

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

asked by anonymous 15.12.2017 / 12:29

1 answer

0

Considering the code snippet that uses an array vector, I have the following comments.

typedef struct mercearia { ... } mercearia;

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

struct mercearia produto[5];

can be rewritten as

mercearia produto[5];

Another point is scanf . scanf requires you to enter the address of the memory space you want to store certain information. The & operator accesses this address.

Therefore, your EstruturaCadastro function 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 be given a pointer to mercearia to be able to traverse the array that is passed as a parameter

void ExibirCadastroProdutos(mercearia *x)

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

    
15.12.2017 / 16:18