You need to access the address of obj without & because *obj is already a pointer:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
struct abc {
uint8_t byte_we_want_to_skip;
uint8_t j;
};
int main(void) {
struct abc *obj= malloc(sizeof(struct abc));
uint8_t *j = (uint8_t*)((char *)obj+ sizeof(uint8_t)); // remove the &
*j = 5;
printf("address of obj => %p - value in obj->j: %d\n",obj, obj->j);
printf("address of j => %p - value at j address: %d \n", j, *j);
return 0;
}
output:
address of obj => 0x55bbc5f84260 - value in obj->j: 5
address of j => 0x55bbc5f84261 - value at j address: 5
Or you can have your struct on the stack and then you can use & to access its address:
int main(void) {
struct abc obj;
uint8_t *j = (uint8_t*)((char *)&obj+ sizeof(uint8_t));
*j = 5;
printf("address of obj => %p - value in obj.j: %d\n", &obj, obj.j);
printf("address of j => %p - value at j address: %d \n", j, *j);
uint16_t *a = (uint16_t*)((char *)j - 1);
printf("address of a => %p - value at a address: %x \n", a, *a); // will print j * 100 in hexa
*a = 42 | 3 << 8;
printf("\naddress of obj => %p - value in obj.i: %d\n", &obj, obj.i);
printf("address of obj => %p - value in obj.j: %d\n", &(obj.j), obj.j);
printf("address of j => %p - value of j: %d \n", j, *j);
return 0;
}
I added an uint16_t to illustrate a bit more, output:
address of obj => 0x7ffecc5fa536 - value in obj.j: 5
address of j => 0x7ffecc5fa537 - value at j address: 5
address of a => 0x7ffecc5fa536 - value at a address: 500
address of obj => 0x7ffecc5fa536 - value in obj.i: 42
address of obj => 0x7ffecc5fa537 - value in obj.j: 3
address of j => 0x7ffecc5fa537 - value of j: 3
NB: We get 0x0500 because of little endianness: inside the cpu bytes are 0x00 followed by 0x05, but the variable a will read bytes values from right to left