fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct { int num_tel, nbr_appel, cout; } Client;
  5.  
  6. void initClient(Client *pClient, int num_tel, int nbr_appel, int cout);
  7. Client* createData();
  8.  
  9. #define NBCLIENT 20
  10.  
  11. void initClient(Client *pClient, int num_tel, int nbr_appel, int cout)
  12. {
  13. pClient->num_tel = num_tel;
  14. pClient->nbr_appel = nbr_appel;
  15. pClient->cout = cout;
  16. }
  17.  
  18. Client* createData()
  19. {
  20. Client *clients = malloc(NBCLIENT * sizeof (Client));
  21. /** @bug check for NULL pointer missing! */
  22. for (int i = 0; i < NBCLIENT; ++i) {
  23. int numeroTel = 600000000 + (rand() % NBCLIENT);
  24. int prixAppel = (rand() % 400) + 1;
  25. initClient(clients + i, numeroTel, 1, prixAppel);
  26. /* &clients[i] would've worked as well as clients + i */
  27. }
  28. return clients;
  29. }
  30.  
  31. int main()
  32. {
  33. Client *clients = createData();
  34. for (int i = 0; i < NBCLIENT; ++i) {
  35. Client *pClient = clients + i; /* or: &clients[i] would work as well */
  36. printf("%2d.: %d, %d, %d\n",
  37. i, pClient->num_tel, pClient->nbr_appel, pClient->cout);
  38. }
  39. free(clients);
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
 0.: 600000003, 1, 87
 1.: 600000017, 1, 116
 2.: 600000013, 1, 336
 3.: 600000006, 1, 93
 4.: 600000009, 1, 222
 5.: 600000002, 1, 28
 6.: 600000010, 1, 60
 7.: 600000003, 1, 327
 8.: 600000000, 1, 227
 9.: 600000012, 1, 137
10.: 600000011, 1, 169
11.: 600000007, 1, 30
12.: 600000002, 1, 331
13.: 600000002, 1, 324
14.: 600000007, 1, 336
15.: 600000009, 1, 203
16.: 600000002, 1, 259
17.: 600000009, 1, 168
18.: 600000013, 1, 57
19.: 600000011, 1, 43