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:44

The problem lies in the fact that you cannot just use a pointer without initialising it to a variable of that same datatype, which in this is a entity variable. Without this, the pointer will point to some random memory location containing some garbage values. You will get segmentation faults when trying to play with such pointers.

The second thing to be noted is that you can't directly assign strings to variables with the assignment operator(=). You have to use the strcpy() function which is in the string.h header file.

The output of the code is: 100 blabla......etc

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>


typedef struct
{
      unsigned long attr;
      char fileName[128];
} entity;

void main()
{
        unsigned long int attribute = 100;
        char *fileNameDir = "blabla....etc";

        entity struct_entity;
        entity *aEntity = &struct_entity;

        aEntity->attr = attribute;
        strcpy(aEntity->fileName, fileNameDir);

        printf("%ld %s", struct_entity.attr, struct_entity.fileName);
}
查看更多
对你真心纯属浪费
3楼-- · 2020-02-15 02:45

You should be copying the filename string, not changing where it points to.

查看更多
女痞
4楼-- · 2020-02-15 02:48

Use strncpy():

strncpy( aEntity->fileName, fileNameDir, sizeof(entity.fileName) );
aEntity.fileName[ sizeof(entity.fileName) - 1 ] = 0;

The strncpy() function is similar, except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated. See man page.

查看更多
何必那么认真
5楼-- · 2020-02-15 02:51

The major problem is that you declared a pointer to a struct, but allocated no space to it (unless you left some critical code out). And the other problems which others have noted.

查看更多
Anthone
6楼-- · 2020-02-15 02:57

Are you writing C or C++? There is no language called C/C++ and the answer to your question differs depending on the language you are using. If you are using C++, you should use std::string rather than plain old C strings.

There is a major problem in your code which I did not see other posters address:

entity* aEntity;

declares aEntity (should be anEntity) as a pointer to an entity but it is not initialized. Therefore, like all uninitialized pointers, it points to garbage. Hence:

aEntity->attr = attributes;

invokes undefined behavior.

Now, given a properly initialized anEntity, anEntity->fileName is an array, not a pointer to a character array (see question 6.2 in the C FAQ list). As such, you need to copy over the character string pointed to by fileNameDir to the memory block reserved for anEntity->fileName.

I see a lot of recommendations to use strncpy. I am not a proponent of thinking of strncpy as a safer replacement for strcpy because it really isn't. See also Why is strncpy insecure?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

int main(void) {
    int status = EXIT_FAILURE;
    unsigned long attribute = 100;
    char *fileNameDir = "blabla....etc";

    entity *anEntity = malloc(sizeof(*anEntity));

    if ( anEntity ) {
        anEntity->attr = attribute;
        anEntity->fileName[0] = '\0';
        strncat(anEntity->fileName, fileNameDir, sizeof(anEntity->fileName) - 1);
        printf("%lu\n%s\n", anEntity->attr, anEntity->fileName);
        status = EXIT_SUCCESS;
    }
    else {
        fputs("Memory allocation failed", stderr);
    }

    return status;
}

See strncat.

查看更多
迷人小祖宗
7楼-- · 2020-02-15 03:00

For char fileName[128], fileName is the array which is 128 char long. you canot change the fileName. You can change the content of the memory that filename is pointing by using strncpy( aEntity->fileName, fileNameDir, 128 );

查看更多
登录 后发表回答