gpt4 book ai didi

c - C 中返回结构表的函数

转载 作者:行者123 更新时间:2023-11-30 20:59:10 25 4
gpt4 key购买 nike

我有一个结构体调用 Client ,以及一个创建此类结构体的函数 initClient

现在我想实现一个函数来创建此结构的table[20000],但我无法弄清楚返回数据的正确语法。是 Client** 还是 Client[] ?...这是我的代码:client.h:

 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();

和main.c:

#include <stdio.h>
#include <stdlib.h>
#include "client.h"

Client* initClient(int num_tel, int nbr_appel,int cout){
Client *c = (Client *) malloc(sizeof(Client));
c-> num_tel =num_tel;
c-> nbr_appel= nbr_appel;
c-> cout= cout;

return c;
}

Client* createData(){
// Number of clients
#define NBCLIENT 20
Client* tab[NBCLIENT];
Client *c;
int i,numeroTel,prixAppel;

for(i=0;i<NBCLIENT;i++)
{
// Generation of a random num_tel
numeroTel = 600000000+(rand() % NBCLIENT);
prixAppel = (rand() % 400)+1;
c=initClient(numeroTel,1,prixAppel);
tab[i]=c;
}
return tab;
}

int main(int argc, char *argv[])
{
createData();
system("PAUSE");
return 0;
}

最佳答案

根据评论中讨论的内容(并考虑到我给出了一些可能令人困惑的提示),我想展示我看到的满足所有要求的(唯一)解决方案:

#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;
}

因此,为了匹配所需的签名,initClient() 返回的 struct 值被复制到数组 malloc()-ed 中createData()。通过这种方式,free() 对于防止内存泄漏非常重要,因为 initClient() 中完成的 malloc() 的内存是之后既没有使用也没有引用。

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

关于c - C 中返回结构表的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com