How to pass a struct vector by reference parameter?

Asked

Viewed 23,004 times

1

My question is, what is the error of this passage? It seems that the passage (*dadosCliente+i) is incorrect, someone knows why?

struct cliente{
    char nome[50];
    char endereco[50];
}

void addCliente(struct cliente *dadosCliente, int *i){
    printf("qual o nome do cliente?");
    scanf(" %s", (*dadosCliente+i).nome);
    printf("qual o endereço do cliente?");
    scanf(" %s", (*dadosCliente+i).endereco);
    *i=*i+1;
}

void main(){
    int i=0,h=0;
    struct cliente clientes[1000];
    while(h!=1){
        printf("Caso queira sair do cadastro digite 1");
        addCliente(clientes, &i);
    }
}
  • Can you be more specific? What you expected to happen that isn’t happening?

  • (*dataClient+i) appears to be incorrect.

  • Welcome to Stack Overflow in English, we are a question and answer site. In order to be able to better answer your question, you could improve your question a little more?

  • I edited it out.

  • When asking a question about a mistake, remember to tell exactly what the mistake is. If the behavior is unexpected, tell us what you expected and what you got. If there was a compilation error (probably your case), please inform the diagnostic message.

3 answers

7


First you need to melt the i. After that it will be necessary a couple more parentheses:

(*(dadosCliente+*i)).endereco

or

(dadosCliente+*i)->endereco
  • 2

    It is worth adding that it is a scanf, so there would have to be an address there, IE, or remove the * or add & before the variable name.

  • 3

    In this case no, because the address and name are arrays

  • True, I hadn’t noticed that.

1

(scanf(" %s", (*dadosCliente+i).nome);

The changes would need to be these:

(scanf(" %s", (dadosCliente+*i)->nome);

You de-reference the pointer i and then add to the first address of dadosCliente, which deviates with -> and access the name field. Since the name field is a vector, it is only necessary nome, the first address, to save the string, but you could be redundant:

(scanf(" %s", &( (dadosCliente+*i)->(*nome) ));

There are some serious problems in your code...

  1. Endless looping, there will never be change in the value of h
  2. The printf will only scan to the first blank space, the rest will be in the buffer replying subsequent questions
  3. Memory invasions in the name field and in the client vector; The first due to the printf being without limitations, would need something like: printf(" %50[^\n]s", vetChar); The second due to endless looping

Here there is a study material on the printf.

I hope I’ve helped. :-)

0

struct cliente{
    char nome[50];
    char endereco[50];
};//faltava o ponto e virgula

void addCliente(struct cliente *dadosCliente, int *i)//ha duas maneiras de usar o vetor de struct por referencia
{
    printf("qual o nome do cliente?");
    scanf(" %s", dadosCliente->nome);//a primeira muito mais simples
    printf("qual o endereço do cliente?");
    scanf(" %s", (*(dadosCliente+*i)).endereco);// e aqui no caso pra poder dar certo oq pretendia fazer 
    *i++;
}

void main(){
    int i=0,h=0;
    struct cliente clientes[1000];
    while(h!=1){
        printf("Caso queira sair do cadastro digite 1");
        addCliente(clientes, &i);
    }
}

Browser other questions tagged

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