【问题标题】:Pass array of struct and return array of struct using pointers - function使用指针传递结构数组并返回结构数组 - 函数
【发布时间】:2016-03-23 18:01:12
【问题描述】:

我意识到这个问题经常被问到,但我仍然不明白它是如何工作的。我想尝试将我的文件读入我的结构中,在函数内部,并通过指向函数的指针传递结构。我不确定如何编写函数原型。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 90
#define STR 200
#define MAXLEN 40

struct human {
    char name[MAXLEN];
    char surname[MAXLEN];
    int age;
    float weight;
};
int main(int argc, char *argv[]) {
    char *dlim= " ", *end = "\n";
    char *string;
    string = (char *)malloc(sizeof(char) * STR);
    int i = 0, j = 0;
    struct human *man = malloc(sizeof(struct human) * MAX);

    FILE *fin = fopen("data.txt", "r");
    if (fin == NULL) {
        printf("Cannot open file\n");
        exit(0);
    }
    while (fgets (string, STR, fin)){
        read (string, &man[i], dlim, end);
        i++;
    }
    fclose(fin);

    free(string);
    free(man);

  return 0;
}

struct human *man read(char *fstring, struct *man, char *div, char *end){
   int i=0;
   char *tok;

        tok = strtok(string, dlim);
        strcpy(man[i].name, tok);

        tok = strtok(NULL, dlim);
        strcpy(man[i].surname,tok);

        tok = strtok(NULL, dlim);
        man[i].age = atoi(tok);

        tok = strtok(NULL, end);
        man[i].weight = atof(tok);

   return man[i];

}

这个函数应该是什么样子的?我是否正确假设通过使用指针,结构将在 main 中自动更新,而无需在函数中返回某些内容?

函数是否也可以不返回任何内容(void),因为指针的使用会自动传递给 main?

谢谢!

【问题讨论】:

  • 函数 read 必须返回一些东西。您不需要返回结构人类指针。您已经传递了一个指向要填充的结构的指针。如果成功,最好返回 0,如果失败则返回非零错误代码。这是典型的行为。

标签: c arrays function pointers struct


【解决方案1】:

您的代码很接近,但填充人体结构的子程序中的指针逻辑不正确。看看下面的返工和轻微的简化是否能帮助你理解如何通过结构:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXIMUM_HUMANS 90
#define MAXIMUM_INPUT_STRING_LENGTH 200
#define MAXIMUM_STRING_LENGTH 40

struct human {
    char name[MAXIMUM_STRING_LENGTH];
    char surname[MAXIMUM_STRING_LENGTH];
    int age;
    float weight;
};

void initialize_human(char *string, struct human *man, char *delimiter, char *end) {
    char *token;

    token = strtok(string, delimiter);
    strcpy(man->name, token);

    token = strtok(NULL, delimiter);
    strcpy(man->surname, token);

    token = strtok(NULL, delimiter);
    man->age = atoi(token);

    token = strtok(NULL, end);
    man->weight = atof(token);
}

int main(int argc, char *argv[]) {
    char *dlim= " ", *end = "\n";
    char string[MAXIMUM_INPUT_STRING_LENGTH];
    struct human humans[MAXIMUM_HUMANS];

    FILE *fin = fopen("data.txt", "r");

    if (fin == NULL) {
        fprintf(stderr, "Cannot open file\n");
        exit(1);
    }

    int population; // after loop, this contains total body count

    for (population = 0; fgets(string, MAXIMUM_INPUT_STRING_LENGTH, fin); population ++) {
        initialize_human(string, &humans[population], dlim, end);
    }

    printf("population: %d\n", population);
    printf("last added: %s who weighs %f\n", humans[population - 1].surname, humans[population - 1].weight);  // test if we loaded it up correctly

    fclose(fin);

    return 0;
}

【讨论】:

    猜你喜欢
    • 2013-08-02
    • 2010-12-21
    • 1970-01-01
    • 2021-11-02
    • 2019-05-03
    • 2019-11-07
    • 1970-01-01
    相关资源
    最近更新 更多