简体   繁体   English

使用指针传递结构体数组并返回结构体数组-函数

[英]Pass array of struct and return array of struct using pointers - function

I realize this question has been asked often but I still dont really understand how it works. 我知道这个问题经常被问到,但是我仍然不太了解它是如何工作的。 I want to try and read my file into my structures, inside a function and pass the structures through pointers to the function. 我想尝试将文件读入函数内部的结构中,并通过指向函数的指针传递结构。 I am unsure about how to even write the function prototype. 我不确定如何编写函数原型。

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

}

Whats the function meant to look like? 该功能看起来像什么? And am i correct in assuming that through the use of pointers, the struct will be automatically be updated in main, without needing to return something in the function? 我是否正确地假设通过使用指针,该结构将在main中自动更新,而无需在函数中返回任何内容?

Can the function also return nothing (void), because the use of pointers will automatically pass onto main? 由于使用指针会自动传递给main,因此函数还可以不返回任何内容(无效)吗?

Thank you! 谢谢!

Your code is close but the pointer logic in the subroutine that fills in the human structure is incorrect. 您的代码已关闭,但是子程序中填充人为结构的指针逻辑不正确。 See if the following rework, and slight simplification, helps you understand how to pass the structure: 看看下面的返工和略微的简化是否可以帮助您了解如何通过结构:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM