-2

i have seen a block of code which allocates memory pool. A line in the block of code says

*(char**) block = nextblock;

can any one help what is the intention of typecasting a char* block as *(char**)?

4

1 回答 1

0

除了代码可读性之外,我想不出应该进行这种类型转换的原因。

取消引用void**指针是允许的,甚至不被 my 所反对gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52),调用gcc -pedantic dereferencetest.c

struct StructTest {
  char* ptr;
};

int main(void)
{
  char* str = strdup("test");
  char** ptr = &str;
  void** vptr = ptr; // warning here
  char* strRecycled = *vptr; // no warning on dereferencing

  printf("str=[%s]", strRecycled);


  {
    struct StructTest val;
    struct StructTest* structPtr = &val;
    struct StructTest** structPtrPtr = &structPtr;

    vptr = structPtrPtr; // warning here
    structPtr = *vptr; // no warning on dereferencing
  }


  return 0;
}

有关于无效指针类型分配和初始化的警告,但没有取消引用警告。

dereferencetest.c:9: warning: initialization from incompatible pointer type
dereferencetest.c:20: warning: assignment from incompatible pointer type
于 2013-09-05T11:09:33.740 回答