Hi,
I have this struct:

typedef struct empresa {
    char nome_empresa [20];
    MORADA morada_empresa;
    int nif;
    int telefone;
    int anocriacao;
    char cae [20];
    ESTAGIARIO estagiarios [MAX_EST];
    int num_est;
}   EMPRESA;

And an array of this struct:

EMPRESA empresas [MAX_EMP];

My problem is that when i try to, manually assign values to the struct variables i get an error, this is how i do it:

empresas[i].nome_empresa = "a string";

The error i get is:

"incompatibel types in assignment"

I can't figure out what is causing this. Can anyone help, please?
Thanks.

Re: Struct variable assignment 80 80

An array is in fact a constant pointer pointing to a block of memory.
So
char message [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char message [] = "Hello";
is valid : initialisation

message = { 'H', 'e', 'l', 'l', 'o', '\0' };
message = "Hello";
or even
message [] = "Hello";
is not valid : assignment

Use the string class, life will be much easier.

Re: Struct variable assignment 80 80

Try this instead:

//empresas[i].nome_empresa = "a string";
strcpy( empresas[i].nome_empresa, "a string" );
Re: Struct variable assignment 80 80

Try this instead:

//empresas[i].nome_empresa = "a string";
strcpy( empresas[i].nome_empresa, "a string" );

Thanks, that solved the problem :)
I get what i did wrong, now.

Re: Struct variable assignment 80 80

what is virtual?why r u go for virtual
?

commented: Don't post on old threads -1
Re: Struct variable assignment 80 80

no real good re-assignment unless you write one yourself, which is usually what we do, thats why operator overloading is so flexible, it makes those pitfalls disappear completely.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.