According to what has been discussed in the comments (and considering that I gave some probably confusing hints) I want to show the (only) solution I see to match all requirements:
#include <stdio.h>
#include <stdlib.h>
typedef struct { int num_tel, nbr_appel, cout; } Client;
Client* initClient(int num_tel, int nbr_appel, int cout);
Client* createData();
#define NBCLIENT 20
Client* initClient(int num_tel, int nbr_appel, int cout)
{
Client *pClient = malloc(sizeof (Client));
/** @bug check for NULL pointer missing! */
pClient->num_tel = num_tel;
pClient->nbr_appel = nbr_appel;
pClient->cout = cout;
return pClient;
}
Client* createData()
{
Client *clients = malloc(NBCLIENT * sizeof (Client));
/** @bug check for NULL pointer missing! */
for (int i = 0; i < NBCLIENT; ++i) {
int numeroTel = 600000000 + (rand() % NBCLIENT);
int prixAppel = (rand() % 400) + 1;
Client *pClient = initClient(numeroTel,1,prixAppel);
/* copy returned CONTENTS to malloc-ed array element */
clients[i] = *pClient; /* assignment for struct-s is granted */
/* release pClient to prevent memory leaks - it's not anymore needed */
free(pClient);
}
return clients;
}
int main()
{
Client *clients = createData();
for (int i = 0; i < NBCLIENT; ++i) {
Client *pClient = clients + i; /* or: &clients[i] would work as well */
printf("%2d.: %d, %d, %d\n",
i, pClient->num_tel, pClient->nbr_appel, pClient->cout);
}
free(clients);
return 0;
}
Thereby, to match the required signatures the returned struct
value of initClient()
is copied into the array malloc()
-ed in createData()
. As doing it this way, the free()
is important to prevent memory leaks as the memory of malloc()
done in initClient()
is neither used nor referenced afterwards.
Compiled and tested on ideone. Output was:
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
The temporary memory allocated in initClient()
and free-d in createData()
would be something which would bother me. This could be prevented with a slight change of the signature of initClient()
:
#include <stdio.h>
#include <stdlib.h>
typedef struct { int num_tel, nbr_appel, cout; } Client;
void initClient(Client *pClient, int num_tel, int nbr_appel, int cout);
Client* createData();
#define NBCLIENT 20
void initClient(Client *pClient, int num_tel, int nbr_appel, int cout)
{
pClient->num_tel = num_tel;
pClient->nbr_appel = nbr_appel;
pClient->cout = cout;
}
Client* createData()
{
Client *clients = malloc(NBCLIENT * sizeof (Client));
/** @bug check for NULL pointer missing! */
for (int i = 0; i < NBCLIENT; ++i) {
int numeroTel = 600000000 + (rand() % NBCLIENT);
int prixAppel = (rand() % 400) + 1;
initClient(clients + i, numeroTel, 1, prixAppel);
/* &clients[i] would've worked as well as clients + i */
}
return clients;
}
int main()
{
Client *clients = createData();
for (int i = 0; i < NBCLIENT; ++i) {
Client *pClient = clients + i; /* or: &clients[i] would work as well */
printf("%2d.: %d, %d, %d\n",
i, pClient->num_tel, pClient->nbr_appel, pClient->cout);
}
free(clients);
return 0;
}
Compiled and tested again on ideone. Output was:
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