How to make a pointer increment by 1 byte, not 1 unit
20,526
I'd suggest you to create a pointer of char and use it to transverse your struct.
char *ptr = (char*) opt;
++ptr; // will increment by one byte
when you need to restore your struct again, from ptr, just do the usual cast:
opt = (tcp_option_t *) ptr;
Author by
misteryes
Updated on March 24, 2020Comments
-
misteryes about 4 years
I have a structure
tcp_option_t
, which isN
bytes. If I have a pointertcp_option_t* opt
, and I want it to be incremented by 1, I can't useopt++
or++opt
as this will increment bysizeof(tcp_option_t)
, which isN
.I want to move this pointer by 1 byte only. My current solution is
opt = (tcp_option_t *)((char*)opt+1);
but it is a bit troublesome. Are there any better ways?