我有一个结构调用Client,以及一个创建这种结构的函数myList:Array<any>=[{prop:someValue},{prop:someOtherValue}];
。
现在我想实现一个创建此结构initClient
的函数,但我无法弄清楚返回数据的正确语法。是table[20000]
还是Client**
?...
这是我的代码:Client[]
:
client.h
和main.c:
struct sClient
{
int num_tel;
int nbr_appel;
int cout;
};
typedef struct sClient Client;
Client * initClient(int num_tel, int nbr_appel,int cout);
Client *createData();
答案 0 :(得分:1)
根据评论中讨论的内容(并考虑到我提供了一些可能令人困惑的提示),我想展示我认为符合所有要求的(唯一)解决方案:
#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;
}
因此,为了匹配所需的签名,struct
的返回initClient()
值将被复制到malloc()
中的createData()
中。正如这样做,free()
对于防止内存泄漏非常重要,因为malloc()
中initClient()
完成的内存既不会被使用也不会被引用。
在ideone上进行编译和测试。输出是:
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
initClient()
中分配的临时内存和createData()
中的free-d将会让我感到烦恼。只需略微更改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;
}
在ideone上再次进行编译和测试。输出是:
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