Bonjour,
j'ai du mal à passer l'adresse (pointeur) d'une structure en paramètre d'une fonction qui en modifie le contenu. Je veux que les modifications soient prises en compte dans le programme appelant.
Ce code là MARCHE:
Maintenant je veux passer en paramètre la structure DH ( http://www.openssl.org/docs/crypto/dh.html# ) dans le programme suivant mais ça ne marche pas
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include <stdio.h> #include <stdlib.h> typedef struct { int *a; // pointeur d'entiers } myStruct; int main() { void test(myStruct *s); // modifie la valeur de a myStruct *s; s = malloc(sizeof(myStruct)); test(s); printf("s: %i\n", *(*s).a); free((*s).a); free(s); return EXIT_SUCCESS; } void test(myStruct *s) { (*s).a = malloc(sizeof(int)); *(*s).a = 1; printf("s: %i\n", *(*s).a); }
A la sortie de la fonction, les champs de dhAlice sont nuls alors qu'ils ne l'étaient pas dans la fonction test.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <openssl/dh.h> #include <openssl/engine.h> #define PRIME_LEN 64 #define GENERATOR 2 int main() { void printDH(char *name, DH *dh); void test(DH *dh); // modifie les champs de dh DH *dhAlice; srand( (unsigned)time( NULL ) ); dhAlice = DH_new(); test(dhAlice); printDH("Main", dhAlice); // MARCHE PAS :( DH_free(dhAlice); return EXIT_SUCCESS; } void test(DH *dh) { printf("Generating DH params ... this can take a while \n"); dh = DH_generate_parameters(PRIME_LEN, GENERATOR, NULL, NULL); printf("Generating public & private keys ... \n"); DH_generate_key(dh); printDH("Alice", dh); // MARCHE } void printDH(char *name, DH *dh) { //char *BN_bn2dec(const BIGNUM *a); printf("--------------\n"); printf("%s :\n", name); printf("%i bits\n", PRIME_LEN); if(dh->p != NULL) printf("p: %s\n", BN_bn2hex(dh->p)); else printf("p: NULL\n"); printf("g: %s\n", BN_bn2hex(dh->g)); printf("priv_key: %s\n", BN_bn2hex(dh->priv_key)); printf("pub_key: %s\n", BN_bn2hex(dh->pub_key)); printf("\n"); }
merci d'avance[/b]
Partager