simpels problem with Vector and Structs

Asked

Viewed 190 times

3

Hi, I’d like to know why you’re not going, I’ve searched a lot of places but I can’t find

in C

#include <stdio.h>
typedef struct ficha_pessoal{
    int idade;
    char sexo;
    int CPF [11];
    int CPFc [3];
    float salario;
    char nome [40];
} FICHA;
int main(){
    FICHA x;
    x.idade=32;
    x.sexo = 'M';
    x.nome[40]= "JOSE DA SILVA";
    x.salario =850;
    x.CPF[1] = {5,3,1,9,8,7,0,0,,1,4,1};
    printf("%d",x.CPF[1]);
return 0;
}

says so in error

||In Function 'main':| |18|Warning: assignment makes integer from Pointer without a cast [enabled by default]| |20|error: expected Expression before '{' token| ||=== Build failed: 1 error(s), 1 Warning(s) (0 minute(s), 0 Second(s)) ===|

  • Check that the value you are passing to the CPF is correct.

  • "JOSE DA SILVA" is the type const char *, and x.nome is the type char []. Always remember that arrays and pointers are very different types. To copy a string to an array use strncpy(). In this case do: strncpy(x.nome, "JOSE DA SILVA", 40); and do the #include <string.h>

2 answers

1

This way you are doing the assignment, it is only possible in the variable declaration. (or if you make a Function for it).

The first error is because you cannot assign a value like this in the C language.

The second error is syntax, showing that '{}' (keys) do not exist in the assignment of a type of this (integer).

I hope I’ve helped.

Take a better look at this link here: https://pt.wikibooks.org/wiki/Programar_em_C/Vetores

Hug and good studies!

0

As stated above, the mode of assignment variável = [valores], in C, may only be used in the variable declaration.

Another important point is that even if you hadn’t used {} and yes [] And the language allowed the assignment of value in this way, it would still be incorrect.
In doing x.CPF[1] = [valores,aqui] - You are trying to assign an entire array to a single home of an array.

Browser other questions tagged

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