Structure Problem in C

2020-02-15 02:32发布

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

标签: c
12条回答
孤傲高冷的网名
2楼-- · 2020-02-15 02:35

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():

/**
 * In your snippet above, you're just declaring a pointer to entity but not
 * allocating it; is that just an oversight?
 */
entity *aEntity = malloc(sizeof *aEntity); 
...
strcpy(aEntity->fileName, fileNameDir);

or

strncpy(aEntity->fileName, fileNameDir, sizeof aEntity->fileName);

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:

struct entity {unsigned long attr; std::string fileName};

entity *aEntity = new entity;
std::string fileNameDir = "...";
...
entity->fileName = fileNameDir;
查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-15 02:36

You can't assign a pointer to an array. Use strncpy() for copying the string:

strncpy( aEntity->fileName, fileNameDir, 128 );

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:

#define BufferLength 128

typedef struct {
  unsigned long attr;
  char fileName[BufferLength + 1];
} entity;

strncpy( aEntity->FileName, fileNameDir, BufferLength );
*( aEntity->FileName + BufferLength ) = 0;
查看更多
唯我独甜
4楼-- · 2020-02-15 02:36

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/

查看更多
霸刀☆藐视天下
5楼-- · 2020-02-15 02:37

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...

查看更多
贼婆χ
6楼-- · 2020-02-15 02:39

1) The line char* fileNameDir = "blabla....etc" creates a pointer to char 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 example strncpy.

Also note that you have merely allocated a pointer to your struct, but no memory to hold the struct data itself!

查看更多
▲ chillily
7楼-- · 2020-02-15 02:40
  • You're treating a char[] (and a char*, 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 using std::string? That gets your rid of all these problems.
  • You're defining a pointer to some entity, don't initialize it, and then use it as if at the random address it points to was a valid entity object.
  • There's no need to typedef a struct 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:

struct entity {
  unsigned long attr;
  std::string fileName;
};

entity aEntity;
aEntity.attr = 100;
aEntity.filename = "blabla....etc";
查看更多
登录 后发表回答