假设这个程序:
#include <stdio.h>
#include <string.h>
static void ring_pool_alloc(void **p, size_t n) {
static unsigned char pool[256], i = 0;
*p = &pool[i];
i += n;
}
int main(void) {
char *str;
ring_pool_alloc(&str, 7);
strcpy(str, "foobar");
printf("%s\n", str);
return 0;
}
...是否有可能以某种方式避免 GCC 警告
test.c:12: warning: passing argument 1 of ‘ring_pool_alloc’ from incompatible pointer type
test.c:4: note: expected ‘void **’ but argument is of type ‘char **’
...不强制转换为 (void**) (或只是禁用兼容性检查)?因为我非常想保留有关间接级别的兼容性警告......
为什么不更改方法签名,使其returns新的指针而不是通过指针传递它?其实和平常一样malloc
does:
static void * ring_pool_alloc(size_t n) {
static unsigned char pool[256], i = 0;
void *p = &pool[i];
i += n;
return p;
}
int main(void) {
char *str = ring_pool_alloc(7);
strcpy(str, "foobar");
printf("%s\n", str);
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)