【发布时间】:2015-05-04 23:35:32
【问题描述】:
*MyFile.h*
typedef char* dado_t;
typedef struct elemento elemento;
typedef struct lista2 lista2;
*MyFile.c*
struct elemento{
dado_t str;
elemento* ant;
elemento* prox;
};
struct lista2{
elemento* primeiro;
elemento* ultimo;
elemento* corrente;
};
void_insert(lista2* l, dado_t d){
elemento* novo = malloc(sizeof(elemento*));
novo->str = malloc(strlen(d) * sizeof(char));
novo->str = d;
l->corrente = novo;
l->primeiro = novo;
l->ultimo = novo;
novo->prox = NULL;
novo->ant = NULL;
}
dado_t replace(lista2* l, dado_t d){
dado_t retorno = malloc(strlen(corrente->str) * sizeof(dado_t));
retorno = corrente->str;
corrente->str = realloc(corrente->str, strlen(d) * sizeof(char));
l->corrente->str = d;
return retorno;
}
为什么会出现这个错误?因为myel->str 是一个已分配malloc() 的指针。为什么错误?如果 realloc()
观察:
【问题讨论】:
-
你在 malloc 之后将
myel->str设置为 d -
@Dinesh 是的,但是错误发生在编译器到达那里之前... =s
-
您是否想分享更多代码,因为调用顺序不清楚。也许 - 在 realloc 之前 - 只需打印 myel->str 和 d - 可能会提供一些线索
-
您不能将字符串分配给
myel->str,您必须使用strcpy。 (并为您的strlen行中的零终止符保留空间!) -
@Jongware 既然你给了我答案,那不是更好吗?