what does ntohl(*(uint32_t*) …)do?

后端 未结 2 684
夕颜
夕颜 2021-01-24 11:17
ip=ntohl(*(uint32_t*)PQgetvalue(result, i, 0));

What is the meaning of this code segment?

My guess is that this code takes an input from Postgr

2条回答
  •  野性不改
    2021-01-24 12:01

    ntohl means "network to host long." It (presumably) converts an integer type from network (big-endian) to host byte ordering. Be careful when using this method, however, if you are unfamiliar with the endianess of your machine. If you have a little-endian machine and use ntohl on data which is already in little-endian format (i.e. wasn't sent in big-endian or otherwise) you may have problems.

    *(unit32_t*) is a casting to a 32-bit unsigned integer pointer (the (unit32_t*) part) and then the preceeding * is the dereference operator on that pointer.

    EDIT

    Here is a good reference for endianess as pointed out in the comments below by njr: http://en.wikipedia.org/wiki/Endianness

提交回复
热议问题