I guys I have this struct
typedef struct objeto livro;
struct objeto
{
char titulo[100], autor[100];
int cota;
};
And this code:
int main()
{
FILE *f;
int tam=5, i=0;
livro c[5];
char a={"a"};
f == fopen("bib.dat", "wb");
if(f == NULL)
{
printf("erro ao abrir ficheiro");
}
for(i=0; i<tam; i++)
{
(*c).titulo=a;
(*c).autor=a;
(*c).cota=i;
fwrite(c, sizeof(c), 1,f);
}
return 0;
}
but it says error:assignment to expression with array type on:
(*c).titulo=a;
(*c).autor=a;
I have tried everything I've seen here in the posts my i cant make it work
ais a single char.titulois an array of chars. Therefore the error says you can’t assign them, which is correct. You probably want to use some variant ofstrncpy()memcopy, but never try to assign to an array. BTW,char a={"a"};is a horror:ais a single character while "a" is a string literal that is the const array{'a', '\0'}. Please usechar a = 'a';for a single char orchar a[] = "a"for a C string.char a={"a"};is probably giving you a compiler warning, if not an error. If you wantato be an initialized array of characters containing a string, declare it aschar a[] = "a";. And as pointed out by Sami Kuhmonen, you cannot use an array as the left side of an assignment.c[i]instead of(*c)(which is the same asc[0])?