1. ## Pointer arithmetic

What's the correct way to do pointer arithmetic in this example ?

Say I store a struct in a buffer at some offset and then want to get a pointer to it. e.g.

Code:
```int offset;
sometype mystruct, *ptr;
char buff[32768];

memcpy(buff[offset], mystruct, sizeof(mystruct));```
Is the pointer

ptr = (mystruct *)buffer[offset];

or

ptr = (mystruct *)buffer + offset;

or

ptr = (mystruct *)((&buffer[0]) + offset);

or something else ? I'm getting confused - and coredumps ...

thks

2. Code:
```int offset;
sometype mystruct, *ptr;
char buff[32768];

memcpy(&buff[offset], &mystruct, sizeof(sometype ));```
or

Code:
```int offset;
sometype mystruct, *ptr;
char buff[32768];

memcpy(bufd + offset, &mystruct, sizeof(sometype ));```

3. Sorry - didn't make myself clear enough.

Assume you can't change the code I posted, but I need a pointer to the place in the buffer where the stored struct starts.

Is this legal ??

ptr = (sometype *)buffer[offset];

Should I then be able to write something like

printf("%d\n", ptr->some_int_member);

??

TIA

4. >Assume you can't change the code I posted
Code:
`memcpy(buff[offset], mystruct, sizeof(mystruct));`
But it's got bugs.
Code:
`memcpy(&buff[offset], &mystruct, sizeof(mystruct));`
You see, buff[offset] is a char, not a pointer; and mystruct is a struct, not a pointer. Since memcpy takes pointers for its first two arguments, if we leave it the way you posted, we'd likely to be writing to wherever from la-la land.
Code:
```#include <stdio.h>
#include <string.h>

struct mytype
{
char   name[20];
int    i;
double d;
};

int main(void)
{
char buffer[128];
struct mytype myvar = { "name", -1, 3.14 }, *ptr;
int offset = 24;

ptr = (struct mytype*)&buffer[offset];
memcpy(ptr, &myvar, sizeof(myvar));

printf("ptr->name = \"%s\"\n", ptr->name);
printf("ptr->i    = %d\n",     ptr->i);
printf("ptr->d    = %lf\n",    ptr->d);

return 0;
}

/* my output
ptr->name = "name"
ptr->i    = -1
ptr->d    = 3.140000
*/```

5. Thanks to all for the suggestions and apologies for posting c**p code. I didn't want to post hundreds of lines of real code and came up with a buggy example :-(

The alignment problem looks to be the most probable cause of my core dumps, so I'll have to find some other way of doing this.

D
#