I'm having trouble assigning a value to a variable of type char
of a struct
I'm doing the following
#include <stdio.h>
typedef struct Animal{
char nome[5]; // indiquei que a variavel nome tem 5 caractes
int idade;
}Cachorro;
int main(){
Cachorro Dog;
Dog.idade = 9;
Dog.nome = "Salfr"; // tento atribuir "Salfr" a minha variavel
printf("'%s' '%d'", Dog.nome, Dog.idade);
return 0;
}
Only an error is occurring from Segmentation Failure
When I compile test.c|11|warning: assignment makes integer from pointer without a cast|
appears
and when I run it appears Segmentation Fault
Now when I point out that my variable name and pointer variable works normally it goes below
#include <stdio.h>
typedef struct Animal{
char *nome;
int idade;
}Cachorro;
int main(){
Cachorro Dog;
Dog.idade = 9;
Dog.nome = "Salfr";
printf("'%s' '%d'", Dog.nome, Dog.idade);
return 0;
}
When compiling and running I get the following return
'Safari' '9'
My problem is related to the type char for this question of my question Is there any problem in assigning a value to a pointer? I know this method is not advisable but it was the only mode that worked so far, anyone knows why it is happening Segmentation Fault
in first example?