如何使函数 return 成为指向数组结构的指针

How to make a function return a pointer to array struct

我正在用 C 编写一个函数,它应该 return 一个指向数组结构的指针。 结构是

struct corr{
  char nome[128];
  char cognome[128];
  char password[10];
  float importo;
};
typedef struct corr correntista;

return指向这个结构的指针的函数

correntista *max_prelievo(FILE *fcorrentisti, FILE *fprelievi){
    correntista *corr_max[2];
    corr_max[0] = (correntista *)malloc(sizeof(correntista));
    corr_max[1] = (correntista *)malloc(sizeof(correntista));
    /*.........*/
    return *corr_max;
}

在主程序中,我想按以下方式打印 returned 值:

*c_max = max_prelievo(fc, fp);
printf("Correntista con max prelievi:\n");
printf("Nome: %s\n", c_max[0]->nome);
printf("Cognome: %s\n", c_max[0]->cognome);
printf("Max prelievo: %f\n\n", c_max[0]->importo);

printf("Correntista con max versamenti:\n");
printf("Nome: %s\n", c_max[1]->nome);
printf("Cognome: %s\n", c_max[1]->cognome);
printf("Max versamento: %f\n", c_max[1]->importo);

但只有第一个结构 c_max[0] 具有预期值。 c_max[1] 有垃圾值。我应该在程序中更改什么?

请记住,您拥有的是指针数组,因此当您退出函数时,该数组超出范围。有一个指向此目的的指针,如下所示。

correntista **max_prelievo(FILE *fcorrentisti, FILE *fprelievi){
    correntista **corr_max = malloc(sizeof(correntista *) * 2);
    corr_max[0] = malloc(sizeof(correntista));
    corr_max[1] = malloc(sizeof(correntista));
    /*.........*/
    return corr_max;
}

调用应该是

correntista **c_max = max_prelievo(fc, fp);

有多种解决方案,但它们都需要彻底检查您的职能运作方式。也许最好的解决方案是考虑 scanf 是如何工作的;它 return 没有对象,而是仅对传递给它的参数指向的对象进行操作。因此:

void max_prelievo(correntista *corr_max, FILE *fcorrentisti, FILE *fprelievi){
    /* NOTE: You should make a habit of separating your allocation from this logic;
     *       it makes debugging and testing so much easier... */

    corr_max[0] = (correntista) { .nome = "Michael",
                                  .cognome = "Anderson",
                                  .importo = 42.0 };

    corr_max[1] = (correntista) { .nome = "Undefined",
                                  .cognome = "Behaviour",
                                  .importo = -1.0 };


    /*.........*/
}

int main(void) {
    correntistia c_max[2] = { 0 };
    FILE *f1 = fopen(...), *f2 = fopen(...);

    max_prelievo(c_max, f1, f2);

    printf("Correntista con max prelievi:\n");
    printf("Nome: %s\n", c_max[0].nome);
    printf("Cognome: %s\n", c_max[0].cognome);
    printf("Max prelievo: %f\n\n", c_max[0].importo);

    printf("Correntista con max versamenti:\n");
    printf("Nome: %s\n", c_max[1].nome);
    printf("Cognome: %s\n", c_max[1].cognome);
    printf("Max versamento: %f\n", c_max[1].importo);
}

What scanf does return是一个整数,表示成功。也许,如果您将来要使用 return 值,您可以求助于使用类似的模式吗?看看好的一面:这里没有 mallocfree,您不可能泄漏任何内存;代码尽可能简单。


或者,尽管看起来很可怕,但如果您必须求助于使用这种模式,那么减少对 malloc 的调用将会很好地为您服务。考虑这样的事情:

correntista *max_prelievo(FILE *fcorrentisti, FILE *fprelievi) {
    correntista *corr_max = malloc(2 * sizeof *corr_max);

    corr_max[0] = (correntista) { .nome = "Michael",
                                  .cognome = "Anderson",
                                  .importo = 42.0 };

    corr_max[1] = (correntista) { .nome = "Undefined",
                                  .cognome = "Behaviour",
                                  .importo = -1.0 };

    /*.........*/

    return corr_max;
}

main 入口点将 几乎 与我之前给出的示例相同...除了一件事:不要忘记free 那段记忆! :(