c - 函数中的指针出现 "must be modifiable lvalue"错误

标签 c pointers struct dynamic-memory-allocation

我有一个小问题 - 由于某种原因,当我尝试将内存重新分配给指向结构指针的指针以将内存块的数量增加一个用于函数中的额外结构时,Intellisense 告诉我“表达式必须是一个可修改的左值”。问题是,如果我尝试分配给 input 本身,一切都很好 - 问题是当我尝试重新分配给 input + 1 时。

正在使用的结构:

struct Entry_t
{
    char *word;
    Year **years;
    char **syn;
    Meaning **mean;
    Entry *next;
};

struct Mean_t
{
    char *word;
    Entry **enword;
    Meaning *next;
};

功能:

/*
| Function: IsInMean
| Action: Checks whether the meaning already exists
| Input: Master meaning struct, the string
| Returns: The address of the necessary place if yes, NULL otherwise
*/
Meaning *IsInMean(Meaning *MEANS, char *str)
{
    if(MEANS)
    {
        if(strcmp(MEANS->word,str) == 0)
            return MEANS;
        else
            return IsInMean(MEANS + 1,str);
    }
    else
        return MEANS;
}

/*
| Function: FindMeanPlace
| Action: Checks where to wedge the meaning.
| Input: The master Meanings dictionary, the string
| Returns: The address of the place where to wedge, NULL if all are bigger.
*/
Meaning *FindMeanPlace(Meaning *MEANS, char *str)
{
    int cmp;
    if((cmp = strcmp(MEANS->word,str)) > 0)
        return NULL;
    else if(cmp < 0 && strcmp(MEANS->next->word,str) > 0)
        return MEANS;
    else
        return FindMeanPlace(MEANS + 1,str);
}

/*
| Function: NewMean
| Action: Creates and initializes a new meaning struct
| and places in it a new entry
| Input: Standard input, new entry, string with the meaning
| Returns: The address of the new meaning struct
*/
Meaning *NewMean(STANDARD_INPUT,Entry *new_e, char *str)
{
    Meaning *temp = (Meaning*)malloc(sizeof(Meaning));
    InitMean(temp);
    temp->word = (char*)calloc(strlen(str) + 1,sizeof(char));
    strcpy(temp->word,str);
    *(temp->enword) = new_e;
    return temp;
}

/*
| Function: SetMean
| Action: Sets the meanings field of an entry
| Input: Standard input, address of the new entry and
| the appropriate sub-string
| Returns: nada
*/
void SetMean(STANDARD_INPUT, Entry *new_e, char *str)
{
    char *cutout, delim[] = ",_";
    char **temp = NULL;
    int len = 0, cmp, index;
    Entry **entemp, *etemp;
    Meaning *input, *mtemp;
    cutout = strtok(str,delim);
    while(cutout)
    {
        temp = (char**)realloc(temp,(len + 1)*sizeof(char*));
        if(!temp)
            Pexit(STANDARD_C);
        *temp = (char*)calloc(strlen(cutout) + 1,sizeof(char));
        if(!(*temp))
            Pexit(STANDARD_C);
        strcpy(*temp,cutout);
        temp++;
        len++;
        cutout = strtok(NULL,delim);
    }
    QsortCust(STANDARD_C,temp,len + 1);
    while(temp)
    {
        index = 0;
        if(input = IsInMean(MEANS,*temp))
        {
            entemp = input->enword;
            if(strcmp(entemp[0]->word,new_e->word) > 0)
                //entemp + 1 = (Entry**)realloc(entemp,sizeof(entemp) + sizeof(Entry*));  "expression must be modifiable lvalue"
            while(entemp + index)
            {
                if((cmp = strcmp((entemp[index])->word,new_e->word)) < 0 && strcmp((entemp[index + 1])->word,new_e->word) > 0)
                {
                    //(entemp + index + 1) = (Entry**)realloc(entemp + index,sizeof(entemp + index) + sizeof(Entry*));  "expression must be modifiable lvalue"
                    //if(!(entemp + index + 1))
                    //  Pexit(STANDARD_C);
                }
                else if(cmp <0)
                {
                    index++;
                }
            }
        }
        else
        {
            input = FindMeanPlace(MEANS,*temp);
            mtemp  = input->next;
            input->next = NewMean(STANDARD_C,new_e,*temp);
        }
    }
}

STANDARD_INPUT 被定义为主词典(第一个条目*)、年度词典(第一年*)和含义词典(第一个含义*)。 STANDARD_C 定义为来自标准输入的变量名称。

最佳答案

如果你想做的(根据评论)是扩大涂料 vector entemp 本身,但将新空间添加到 beginning 而不是结尾,你不能只需一次操作即可完成。 realloc 必须始终传递一个 un-offsetmalloc(或 calloc 或之前调用 realloc),它总是在末尾添加空格。您必须使用 memmove 自行将所有内容滑下。例如:

nelements += 1;
entemp = realloc(entemp, nelements * sizeof(Entry *));
assert(entemp);
memmove(entemp, entemp+1, (nelements - 1) * sizeof(Entry *));

此外,正如我刚刚注意到的:sizeof(entemp) 返回entemp 指向的内存块的分配大小.它返回指针本身 的大小。无法检索 C 堆上 block 的分配大小;你必须自己追踪它们。这就是上面我的 nelements 变量正在做的事情。

密切相关的强制切线注释:不要强制转换 realloc 的返回值,就像您不强制转换 malloccalloc 的返回值一样。这不仅仅是样式问题:在不需要时进行转换可以隐藏错误!

不太相关但仍然是强制性的风格吹毛求疵:NAMES_IN_ALL_CAPS 按照长期惯例,保留给常量。不要将它们用于变量名。扩展到整个变量/参数声明的魔术宏是 Right Out。

关于c - 函数中的指针出现 "must be modifiable lvalue"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916883/

相关文章:

c# - 我应该如何使用 P/Invoke 将字符串数组传递给 C 库?

mysql - 在静态库中链接 Mysqllib (MAC OS X.8.4, Xcode 4.6.3)

尽管免费,但 C 内存泄漏

char 数组和 char 指针与返回地址之间的冲突点

c - 动态内存结构 : free() failed w/invalid size

c - 指针不指向

c - 字符串比较函数

无法停止使用 !feof() 读取 Unicode 文件

c - 可变参数函数 - 如何获取 var_args 的数量?

C++ 保护类的私有(private) char *