1
votes

Earlier, I asked a similar question, but I've since changed my code. Now the compiler gives me a different warning. This is an example of what my code looks like now:

void *a = NULL;
void *b = //something;
a = *(int *)((char *)b + 4);

When I try to compile, I get "warning: assignment makes pointer from integer without a cast." What does this mean, and what should I do to fix it?

To clarify, I don't want 'a' to point to an address that is 4 bytes greater than 'b' (i.e., a != b+4). In my program, I know that the value stored at ((char *)b + 4) is itself another pointer, and I want to store this pointer in 'a'.

2

2 Answers

1
votes

Since, "the value stored at ((char *)b + 4) is itself another pointer", simply add an explicit cast to the result:

void *a = NULL;
void *b = //something;
a = (void*)*(int *)((char *)b + 4);

Since you're assuming that sizeof(void*)==sizeof(int), the code is not portable, but I am sure you already knew that.

A portable alternative is to use intptr_t instead of int.

1
votes

So there's a pointer value stored at b+4 which you want to load into a? Then treat it as one, and so (b+4) points to a pointer - pointer to a pointer to int is int**.:

  *(int**)((char*)b+4)