void * et cast unsigned short
Bonjour,
Je cherche à travailler avec des données qui sont un paramètre void * d'une fonction, mais je n'arrive pas à faire une affectation avec un type unsigned short. Comme puis-je caster ça correctement ?
Edit : je m'auto-répond :
Si on oublie de pointer la donnée, ça ne peut pas marche, forcément.
Désolé du dérangement.
Je suis sous linux 2.6.13 et je compile avec gcc sous cygwin.
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| ssize_t read( int fd, void *buf, size_t count ) {
ssize_t ret;
ret = hook._read( fd, buf, count );
printf("read response to [0x%04X] at fd=%d", (unsigned short)(*(int *)buf), fd); /* Ca, ça marche */
if ((unsigned short)(*(int *)buf) == 0xFFFF) { /* Ca, ça marche */
((char *)buf)[0] = '\0'; /* Ca, ça marche */
((char *)buf)[1] = '\1'; /* Ca, ça marche */
((unsigned short *)buf) = 0x0023; /* Ca plante */
(* (unsigned short *)buf) = 0x0023; /* Ca, ça marche */
}
} |
Merci pour votre aide par avance.