C tricks

1 Aug 2012

Assigning 8 bytes into a double variable

int amf0_deserialize_number(double *number, unsigned char *input) { int i; for (i=0;i<8;i++) { memcpy((char *)number + i, input + 7 - i, 1); } return 8; }

This assumes that the bytes are in the IEEE 754 double precision format in the network byte order (little endian).

We have to cast a pointer to (char *); otherwise, when you incremented it, it would jump 4 bytes at a time.

Circular typedef

Assume you have:

typedef struct { A a; } B; typedef struct { B b; } A;

This is a circular dependency. You can solve it by:

typedef struct A A; typedef struct { A a; } B; struct A { B b; };

Use `man` to find where a command belongs to

Normally, when you use `memcpy`, there will be a warning about using a built-in function. It's pretty annoying.

In order to eliminate the warning, you should explicit `#include &lt;string.h>. And to find which file to include, just type ``man memcpy``` in your unix shell.

Give it a kudos