0

I am getting a warning as follows on 64 bit system:

warning: cast to pointer from integer of different size when I make the following call:

char** str;
str = (char **)Calloc2D(nObj + 1, 20, sizeof(char))

Here is the actual function to allocate 2D array:

void **Calloc2D(size_t nobj1, size_t nobj2, size_t size) 
{
  void **p1;
  void *p2;
  size_t iobj1;
  char *c2;

  /* Allocate memory for one big array */

  p2 = calloc((nobj1 * nobj2), size);
  if (p2 == NULL) return NULL;

  /* Allocate memory for the first dimension */

  p1 = (void **) calloc(nobj1, sizeof(void *));
  if (p1 == NULL) {
    free(p2);
    return NULL;
  }

  /* Set up the pointers for the first dimesion */

  c2 = (char *) p2;
  for (iobj1 = 0; iobj1 < nobj1; iobj1++) {
    p1[iobj1] = (void *) c2;
    c2 += (nobj2 * size);
  }

  /* Return a pointer to the 2-dimensional array */

  return p1;
}

What I do not understand is why I am getting the above warning thought my function has been fully declared to return void**. My understanding is that in 64 bit systems void** and char** should have the same size (8 bytes). Then why this warning and how to fix it.

srsci
  • 239
  • 5
  • 10
  • There is no need for the cast at all then, since `void *` is automatically converted to any pointer type, you should not need the cast. – Iharob Al Asimi Jan 07 '15 at 00:31
  • BTW I did include the – srsci Jan 07 '15 at 00:34
  • 6
    You probably don't have a prototype of `Calloc2D` before your call site, so your compiler is assuming it returns `int`. – Carl Norum Jan 07 '15 at 00:36
  • @CarlNorum Sure that has to be, because the cast can't be. – Iharob Al Asimi Jan 07 '15 at 00:36
  • No I do have a prototype declared in the corresponding header file as:`void **Calloc2D(size_t nobj1, size_t nobj2, size_t size);` Alsdo when I remove the case then I get the warning that: `warning: assignment makes pointer from integer without a cast` – srsci Jan 07 '15 at 00:40
  • 8
    You understand `void**` is not some universal two-level indirection pointer, right? `void**` is a **specific** pointer type (pointer to `void*`). It is *not* universally convertible to some other pointer type as `void*` is. And that warning after removing the cast indicates there is no declaration or definition of `Calloc2D` prior to usage. If your warning level is high enough you should also be getting an "implicit declaration of function returns `int`" warning as well (or words to that effect). – WhozCraig Jan 07 '15 at 00:42
  • @WhozCraig I did not know that. So how do I handle such cases as I have shown here? I mean how do I effectively cast a `void**` to another type? – srsci Jan 07 '15 at 00:45
  • 1
    @srsci return the `void**` to a var of type `void**`, then convert to the proper secondary type *after* dereference: `void** ppv = Func(...); char* psz = ppv[n];` etc. In said-example, `ppv[n]` equates to a `void*`, which then converts to any other data pointer type. Honestly, I'd have the function return `void*` if you want to avoid said-hassles, but you still need to have your proto or impl known prior to first usage. – WhozCraig Jan 07 '15 at 00:47
  • As a side issue, if `void*` is a pointer to an unknown type, why would `void**` be meaningful? – Weather Vane Jan 07 '15 at 00:51
  • 1
    @WeatherVane to return a sequence of pointers to arbitrary types is one reason, though it would certainly not be common. – WhozCraig Jan 07 '15 at 00:54
  • Void ** is meaningless--just don't use it. Declare Calloc2D to return void *, and make sure it's declared ahead of first use. – Lee Daniel Crocker Jan 07 '15 at 01:16
  • If it says cast FROM INTEGER maybe Calloc2D isn't defined at that point and the compiler assumes that the function returns int... – Mabus Jan 07 '15 at 01:22
  • [don't cast the result of malloc in C, that'll hide your real problem](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – phuclv Jan 07 '15 at 01:23

0 Answers0