How to make a pointer increment by 1 byte, not 1 unit

后端 未结 1 1149
南笙
南笙 2021-02-13 06:00

I have a structure tcp_option_t, which is N bytes. If I have a pointer tcp_option_t* opt, and I want it to be incremented by 1, I can\'t u

相关标签:
1条回答
  • 2021-02-13 07:01

    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;
    
    0 讨论(0)
提交回复
热议问题