Error when assigning variable value to vector in C

Asked

Viewed 338 times

0

I am creating a user register using struct. In it, there’s the CPF field. As it is a very large field to store in number format, I decided to save as a string, as well as in a database where Cpf is saved with varchar.

I do a validation to not allow registering 2 equal numbers. For this, first I save the value in an auxiliary variable and passing the validation I saved in the vector. The problem is when I pass the value of the variable to the vector.

Error on the line where I pass the value of the variable to the vector: vet[i].cpf = cpfAux;

[Error] assignment to Expression with array type

My code:

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

struct colaborador{
    char nome[50];
    char cpf[18];
    char nascimento[11];
    float salario;
};

struct colaborador vet[2];
int i, j;
char nomePesquisa[30];
char cpfAux[18];
int contPesquisa = 0;
bool rep = false;

int cadastra(){


    for(i=0; i<2; i++){ 
        printf("Insira o nome:");
        gets(vet[i].nome);

        printf("Insira o cpf:");
        gets(cpfAux);


            for(j=0; j<2; j++){
                while(strcmp(cpfAux,vet[j].cpf) == 0) {
                    rep = true;
                    printf("ERRO, CPFS IGUAIS!\n\n");

                    printf("Insira o cpf novamente:");
                    gets(cpfAux);                   
                } 

                rep = false;
            }       

            if(rep == false){
                vet[i].cpf = cpfAux;
            }           



        printf("Insira o nascimento:");
        gets(vet[i].nascimento);


        printf("Insira o salario:");
        scanf("%f", &vet[i].salario);               
        fflush(stdin);
    }

}

int pesquisa(){

    printf("Insira um colaborador a ser pesquisado:");
    scanf("%s", &nomePesquisa);

    for(i=0; i<2; i++){     
        if(strcmp(nomePesquisa,vet[i].nome) == 0){
            printf("Nome: %s\n", vet[i].nome);
            printf("CPF: %s\n", vet[i].cpf);
            printf("Nascimento: %s\n", vet[i].nascimento);
            printf("Salario: %2.f\n", vet[i].salario);
            exit(0);
        } else {
            contPesquisa++;
        }
    }

    if(contPesquisa > 0){
        printf("Colaborador nao encontrado.");
    }

}


int main(int argc, char *argv[]) {

    cadastra(); 
    pesquisa(); 

    return 0;
}

I tested the code using int for Cpf and it worked. The problem is when I put 10-12-15 digits. I tried using the type double, but when the first character is zero-0, it does not store.

How can I fix this? Where am I going wrong?

2 answers

2

Instead of:

vet[i].cpf = cpfAux;

You must use:

strcpy(vet[i].cpf, cpfAux);

This method copies the value of cpfAux and assigns it to the vet[i].cpf.

2


In C, it is not possible to copy the contents of buffers containing strings ended in \0 (array types) using the allocation operator =.

The header pattern string.h provides a complete interface for string handling.

This interface has a function called strcpy(), capable of copying the contents of a buffer to another.

To solve your problem, first, you must include the header string.h in your program:

#include <string.h>

Then replace the line where the error occurs with:

strcpy( vet[i].cpf, cpfAux );
  • I didn’t know the string.h, very interesting! I will read about it to learn even more. The code spun! Thank you very much Lacobus.

Browser other questions tagged

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