我有一点问题 - 出于某种原因,当我尝试将内存重新分配给指向结构的指针的指针时,为一个函数中的额外结构增加一个内存块的数量,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被定义为标准输入的变量名称。
答案 0 :(得分:1)
如果你想做什么(根据评论)是放大涂鸦矢量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
的返回值,就像不投放malloc
或calloc
的返回值一样。这不仅仅是一种风格的东西:当你不需要时可以隐藏错误!
不太相关但仍然是强制性的样式挑剔:NAMES_IN_ALL_CAPS
,按照长期惯例,为常量保留。不要将它们用于变量名称。而扩展到整个变量/参数声明的魔术宏是Right Out。