【发布时间】:2014-08-29 19:03:30
【问题描述】:
我正在为重新分配而苦苦挣扎......
strucType mkBggr (structType x, char ch) {
x = realloc(x, 100);
printf("%d", sizeof(x));
}
我认为这应该打印出值 100,但它打印出 8。
这显然与指针有关,但我不知道是什么。我在 x 前面添加了 *s 和 &s,但我似乎不明白。任何帮助表示赞赏!
【问题讨论】:
-
请添加
structType的定义。 -
问题与
realloc无关。删除对realloc的调用,输出保持不变。问问自己x是什么,sizeof是什么意思。 -
顺便说一句,在此代码中使用 realloc 的正确方法是:
structType tmp = realloc(x, 100); if (tmp) { x = tmp; } else { /*...realloc failed, need to keep old value of x...*/ } -
这个问题已经在 SO 上被问过好几次了,我记得自己也回答过好几次了。
-
@hyde 失败总是返回
NULL, butrealloc()/malloc()` 返回NULL并不总是表示失败。考虑何时size == 0