My structure looks as follows:
typedef struct {
unsigned long attr;
char fileName[128];
} entity;
Then I try to assign some values but get an error message...
int attribute = 100;
char* fileNameDir = "blabla....etc";
entity* aEntity;
aEntity->attr = attributes;
aEntity->fileName = fileNameDir;
Compiler tells me:
Error: #137: expression must be a modifiable lvalue aEntity->fileName = fileNameDir;
Why cant I assign here this character to the one in the structure?
Thanks
First of all, is this supposed to be C or C++? The two are not the same or freely interchangeable, and the "right" answer will be different for each.
If this is C, then be aware you cannot assign strings to arrays using the '=' operator; you must either use strcpy() or strncpy():
or
with appropriate checks for a terminating nul character.
If this is C++, you should be using the std::string type for instead of char* or char[]. That way, you can assign string data using the '=' operator:
You can't assign a pointer to an array. Use strncpy() for copying the string:
This will leave the destination not null-terminated if the source is longer than 128. I think the best solution is to have a bigger-by-one buffer, copy only N bytes and set the N+1th byte to zero:
You're trying to use char* as if it was a string, which it is not. In particular, you're telling the compiler to set filename, a 128-sized char array, to the memory address pointed by fileNameDir.
Use strcpy: http://cplusplus.com/reference/clibrary/cstring/strcpy/
You can't assign a pointer to char to a char array, they're not compatible that way, you need to copy contents from one to another, strcpy, strncpy...
1) The line
char* fileNameDir = "blabla....etc"
creates a pointer tochar
and assigns the pointer an address; the address in this case being the address of the text"blabla....etc"
residing in memory.2) Furthermore, arrays (
char fileName[128]
) cannot be assigned to at all; you can only assign to members of an array (e.g.array[0] = blah
).Knowing (1) and (2) above, it should be obvious that assigning an address to an array is not a valid thing to do for several reasons.
What you must do instead is to copy the data that
fileNameDir
points to, to the array (i.e. the members of the array), using for examplestrncpy
.Also note that you have merely allocated a pointer to your struct, but no memory to hold the struct data itself!
char[]
(and achar*
, FTM) as if it was a string. Which is is not. You can't assign to an array, you'll have to copy the values. Also, the length of 128 for file names seems arbitrary and might be a potential source for buffer overflows. What's wrong with usingstd::string
? That gets your rid of all these problems.entity
, don't initialize it, and then use it as if at the random address it points to was a validentity
object.typedef
astruct
in C++, as, unlike to C, in C++struct
names live in the same name space as other names.If you absolutely must use the
struct
as it is defined in your question (it is pre-defined), then look at the other answers and get yourself "The C Programming Language". Otherwise, you might want to use this code: