Je suis capable de "pack" des tableaux de longues chaînes. Maintenant, si je mémcpy la chaîne contenant des tableaux de long, le contenu est perdu. Ici vous avez mon code:Copier des chaînes contenant des longs
#include <stdio.h>
#include <string.h>
int main()
{
unsigned char arr[40];
unsigned char arr2[40];
unsigned int i = 0 ;
long f=0;
for (i = 0 ; i < 5 ; i++) {
f = i + 1 ;
*((long *)arr + i*sizeof(long)) = f ;
}
memcpy(arr2, arr, 40);
for (i = 0 ; i < 5 ; i++) {
f = *((long *)arr2 + i*sizeof(long)) ;
printf("f =%ld from arr2 sizeof %zu\n", f, (size_t)(i*sizeof(long)));
}
for (i = 0 ; i < 5 ; i++) {
f = *((long *)arr + i*sizeof(long)) ;
printf("f =%ld from arr sizeof %zu\n", f, (size_t)(i*sizeof(long)));
}
}
Et ceci est la sortie:
f =1 from arr2 sizeof 0
f =0 from arr2 sizeof 8
f =1 from arr2 sizeof 16
f =140734674541394 from arr2 sizeof 24
f =140734674541702 from arr2 sizeof 32
f =1 from arr sizeof 0
f =2 from arr sizeof 8
f =3 from arr sizeof 16
f =4 from arr sizeof 24
f =5 from arr sizeof 32
Comme vous pouvez le voir, je suis en mesure de lire les valeurs longues que j'ai magasin dans « arr » mais pas de " arr2 ". Une idée?
'i * sizeof (long)' -> '' î' – BLUEPIXY
(long *) arr2 + i * sizeof (long) 'entraînera un comportement indéfini dû à l'accès au tampon hors limites. '(long *) arr2 + i' est déjà indexé dans' sizeof (long) ' – StoryTeller
Merci ** tout ** pour vos réponses et vos commentaires. Malheureusement, je peux accepter seulement l'un d'eux ... – mauro