fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct { int num_tel, nbr_appel, cout; } Client;
  5.  
  6. Client* initClient(int num_tel, int nbr_appel, int cout);
  7. Client* createData();
  8.  
  9. #define NBCLIENT 20
  10.  
  11. Client* initClient(int num_tel, int nbr_appel, int cout)
  12. {
  13. Client *pClient = malloc(sizeof (Client));
  14. /** @bug check for NULL pointer missing! */
  15. pClient->num_tel = num_tel;
  16. pClient->nbr_appel = nbr_appel;
  17. pClient->cout = cout;
  18. return pClient;
  19. }
  20.  
  21. Client* createData()
  22. {
  23. Client *clients = malloc(NBCLIENT * sizeof (Client));
  24. /** @bug check for NULL pointer missing! */
  25. for (int i = 0; i < NBCLIENT; ++i) {
  26. int numeroTel = 600000000 + (rand() % NBCLIENT);
  27. int prixAppel = (rand() % 400) + 1;
  28. Client *pClient = initClient(numeroTel,1,prixAppel);
  29. /* copy returned CONTENTS to malloc-ed array element */
  30. clients[i] = *pClient; /* assignment for struct-s is granted */
  31. /* release pClient to prevent memory leaks - it's not anymore needed */
  32. free(pClient);
  33. }
  34. return clients;
  35. }
  36.  
  37. int main()
  38. {
  39. Client *clients = createData();
  40. for (int i = 0; i < NBCLIENT; ++i) {
  41. Client *pClient = clients + i; /* or: &clients[i] would work as well */
  42. printf("%2d.: %d, %d, %d\n",
  43. i, pClient->num_tel, pClient->nbr_appel, pClient->cout);
  44. }
  45. free(clients);
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 4496KB
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