What does mean?

Hello cplusplus guys,i saw these from a source code but i don't know what does mean, can someone explain me?

struct tcpheader *tcp = (struct tcpheader *) (buffer + sizeof(struct ipheader));
Last edited on
tcp is a pointer to a tcpheader. It is pointing to your buffer minus the ipheader (so probably just the data.

struct tcpheader *tcp You are making a pointer to a tcpheader struct
= you are initializing the pointer to an address
(struct tcpheader *) regardless of what type it is, you're going to treat it as a tcpheader
(buffer + sizeof(struct ipheader)) The address is going to be part of the buffer. The buffer is a pointer to a structure that contains a header, then data. We're not interested in the header to so we skip that part by adding the size of the header to the buffer pointer.
Last edited on
This is a piece of C code (not C++, although it would compile with a C++ compiler).

It takes a pointer to the beginning of (most likely) some byte array called "buffer", skips as many bytes as there are in the struct "ipheader", and reinterprets the resulting pointer as if it's pointing at an instance of struct "tcpheader". The intent is apparently to conveniently access parts of the TCP header in a data packet received from the network. In practice, this may not work on a sufficiently aggressively optimizing compiler, due to pointer aliasing violations.
Last edited on
thank u Stewbond & Cubbi.
Last edited on
Topic archived. No new replies allowed.