Passage by struct vector reference

Asked

Viewed 565 times

0

I have to do an exercise where I get a struct and a vector following this struct with a size N of houses (which I called in the code of QTD) entered by the user.

After this the program allows to display this list of entered data, however, my problem is in passing by reference of a vector of structs. That is, how does it work? It is a reference passage of any vector?

My idea is to create a function preencher() and within it carry out this filling, follows the code:

typedef struct {

    int id;
    char nome[200];
    char endereco[100];
    char cidade [30];

}Struct1 ;

Struct1 vetorStruct1[QTD]; // vetor de struct que deve ser utilizado 

void preencher( struct Struct1 *vetorStruct1[], int QTD ){ //LINHA NA QUAL ESTOU EM DÚVIDA!!!


int i;

printf("Insira a quantidade de funcionários que serão cadastrados: ");
scanf("%d",&QTD );
printf("\nQTD:%d\n",QTD);

system("pause");
system("cls");


for(i=0; i<QTD; i++) {

printf(" Digite o ID do funcionário: ");
scanf("%d*c", &vetorStruct1[i].id);

printf(" Digite a nome do funcionário: ");
scanf("%s*c", &vetorStruct1[i].nome);

printf(" Digite o endereco do funcionário: ");
scanf("%s*c", &vetorStruct1[i].endereco);

printf(" Digite o cidade do funcionário: ");
scanf("%s*c", &vetorStruct1[i].cidade);

printf(" DADOS DO FUNCIONARIO DE ID %d CADASTRADOS! \n",vetorStruct1[i].id);

system("pause");
system("cls");

}
}

int main() // será somente um switch case entre o preencher e o imprimir.
  • I don’t understand your question or if you are having problems. You have several questions, but none very specific except the second one that the answer is yes. Where you allocated the vector?

  • How does this reference passage by vector work? That’s what I don’t know very well, I’m studying in race and some things pass without understanding..

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

I decided to help because there are an extraordinary number of errors in the code. The ideal is to learn one thing at a time, so just make a mistake, learn from it, move on to the next and there evolves the knowledge. When you try to do what you’re not ready for, you make a lot of mistakes and you don’t learn from them. One of the mistakes is not putting the code in a more organized way to help better. Programming is a lot of organization, who can not do this will have difficulties in the profession.

#include <stdio.h>

typedef struct {
    int id;
    char nome[201];
    char endereco[101];
    char cidade [31];
} Pessoa;

void preencherPessoas(Pessoa pessoas[], int qtde) {
    for (int i = 0; i < qtde; i++) {
        printf(" Digite o ID do funcionário: ");
        scanf("%d", &pessoas[i].id);
        printf(" Digite a nome do funcionário: ");
        scanf("%s", pessoas[i].nome);
        printf(" Digite o endereco do funcionário: ");
        scanf("%s", pessoas[i].endereco);
        printf(" Digite o cidade do funcionário: ");
        scanf("%s", pessoas[i].cidade);
        printf(" DADOS DO FUNCIONARIO DE ID %d CADASTRADOS! \n", pessoas[i].id);
    }
}

int main() {
    printf("Insira a quantidade de funcionários que serão cadastrados: ");
    int qtde;
    scanf("%d", &qtde);
    printf("\nQTD: %d", qtde);
    Pessoa pessoas[qtde];
    preencherPessoas(pessoas, qtde);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I gave a name that means something in the kind of structure created. Code is expression, it’s important to give good names to things to better understand what you’re doing. Every experienced programmer does this, only newbie doesn’t appreciate it.
  • I put an extra byte in strings for it fits the terminator. The sizes are absurdly large and waste a lot of space and any larger amount of data will burst to stack. I won’t go into details, but this whole idea of organization of struct is wrong and real codes are not like that. It serves only to understand other things in this small code, keep this in mind.
  • Do not declare global variables, unless you have a very large domain of what you are doing, only declare local variables.
  • Better name the function. What would happen if you had to fill something else?
  • If you created a type then use it do not use the struct to declare variables and parameters.
  • No need to pass the array by reference because it is already a reference. I would only need to pass like this if I were to exchange the whole object, which is not the case, including nor can I do it in the programmed form.
  • You don’t need to declare the variable before its use, in fact this makes the code less readable
  • Names using ALL_CAPS notation should only be used for macros, and should still be avoided. And don’t need to abbreviate so much, use a more common abbreviation.
  • A function that must fill in people’s data should not ask how many people will be in total. This is the responsibility of another function.
  • If the die is a string then he’s already a array, so it is already a reference and the scanf() asks for a reference, so you do not need to create another reference when you pass this data, is of course already what you expect.
  • I created a main() that makes sense.

If you don’t understand something is because you skipped too many steps. Here on the site you have almost everything you need to know before continuing.

Browser other questions tagged

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