1. ## Pointer arithmetic question

I need a little help with manipulating pointers. Here is the problem. I have two pointers declared as follows:

Header is a struct in the program I'm working on. At this point in my program h1 already points to a location in memory and h2 is currently null. Now I want to make h2 point to a particular amount of bytes past h1 in memory. However, this displacement past h1 is not an integer multiple of the amount of bytes of a Header struct. For example, I need h2 to point to exactly 32 bytes past h1, but Headers are 12 bytes, so I cannot use regular pointer arithmetic (e.g. h2 = h1 + 3 would make h2 point to 36 bytes past h1, but that's the closest I can get to 32).

Does anyone have any ideas? Thanks for any possible input or advice.

2. will typecasting work? Why do you need this? If it is so the structs will be next to eachother remember that a structure has padding

3. >Does anyone have any ideas?

With caution flags...
Code:
```#include <stdio.h>

struct sType
{
char element[12];
};

int main(void)
{
struct sType object = {0};
struct sType *h1 = &object;
struct sType *h2 = (char*)h1 + 32;
printf("&object = %p, h1 = %p, h2 = %p\n",
(void*)&object, (void*)h1, (void*)h2);
return 0;
}

/* my output
&object = 0012FF80, h1 = 0012FF80, h2 = 0012FFA0
*/```

4. The program is supposed to be a memory management program.

h1 points to a header. Directly following that header is a free chunk of memory that a user requested to use which I allocated for him.

The entire thing (header + memory requested) is 2^k bytes where k is some integer. I need to split that whole thing into halves. So h1 will still point to the same place, but h2 will now point to the middle of that 2^k block, and the whole block has essentially become two smallers ones which I can now use.

So say the header + free memory for the user is 2^5 = 32 bytes. What I want to do is have h2 point to 2^(5 - 1) = 16 bytes past where h1 points to. Then h1 and h2 would each point to 16 byte blocks of memory, the beginnign 12 of each which would be a header struct. The remaining 4 bytes each would be for the user who requested the memory.

I hope I explained that well enough.

Once I point h2 to that right location I will type-cast it as a Header pointer but how do I get h2 to point to that location?

I'm thinking something along the lines of:

h2 = (Header *)(&h1 + #bytes);

or something to that effect, but I'm not quite sure. Thanks.

5. Thanks Dave...i'm gonna give that a whirl.

When adding 32 does that add 32 bytes or 32 bytes * sizeOf(char)?

6. >When adding 32 does that add 32 bytes or 32 bytes * sizeOf(char)?

Yes. (Since sizeof(char) is 1 by definition, both would be the same.)

7. Yes I just realized that! Thanks for the help!