我有一个小问题-出于某种原因,当我试图将内存重新分配给指向结构指针的指针以将函数中额外结构的内存块数量增加一个时,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);
        }
    }
}

标准输入被定义为主字典(第一个条目*)、年度字典(第一年*)和语义字典(第一个含义*)。标准C定义为标准输入中变量的名称。

最佳答案

如果要做的(每个注释)是放大摄影向量entemp本身,但将新空间添加到开头而不是结尾,则不能通过单个操作完成此操作。必须始终向realloc传递从malloc接收的未偏移指针(或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堆上块的分配大小;您必须自己跟踪它们。这就是上面我的nelements变量所做的。
密切相关的强制切线注释:不要投射realloc的返回值,就像你没有投射malloccalloc的返回值一样。这不仅仅是一个风格的东西:当你不需要隐藏虫子的时候铸造!
不太密切相关,但仍然是强制性的风格挑剔:NAMES_IN_ALL_CAPS是,根据长期的惯例,保留常数。不要将它们用于变量名。而扩展到整个变量/参数声明的魔术宏则是正确的。

关于c - 函数中的指针出现“必须是可修改的左值”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20916883/

10-13 07:17