将结构体数组传递给C函数

时间:2018-11-18 23:38:32

标签: c function struct

通过引用将结构传递给函数时,在结构数组前(在函数参数中)是否需要*?我认为我们不这样做的原因是,必须通过数组才能传递第一个对象所在的地址。

我觉得我很幸运,我的代码正在运行:

#include <stdio.h>

struct member {
    char lastName[30];
    char gender;
    int age;
};

void readAndUpdate(struct member *people[]); 

// begin main function
int main(void){

    struct member *people[30]; 

    readAndUpdate(people);


} // end main function

// begin function which reads a .dat file and propogates the array with the data in the .dat file
void readAndUpdate(struct member *people[]){



}

在注释程序的帮助下,我做了一些代码工作,下面的代码可以正常工作。我不小心创建了一个指针数组。

#include <stdio.h>
#define MAXPEOPLE 3 

struct member {
    char lastName[30];
    char gender;
    int age;
};

void readAndUpdate(struct member *person, size_t maxpeople); 
void populateDatFile();
void displayMembers(struct member *person, size_t maxpeople);

// begin main function
int main(void){

    struct member people[2]; 

    populateDatFile(); // program will first populate the .dat file with the given specs

    readAndUpdate(people, MAXPEOPLE);

    printf("The data was read and input as follows:\n\n");
    displayMembers(people, MAXPEOPLE);


} // end main function

// function which displays the entire array of struct members
void displayMembers(struct member *person, size_t maxpeople){

    int i=0;

    for (i=0;i<3;i++){

    printf("%s ", person[i].lastName);
    printf("%c ", person[i].gender);
    printf("%d ", person[i].age);
    printf("\n");
    }

} // end displayMembers function

// function which loads the .dat file with hardcoded structs
void populateDatFile(){

    struct member person1={"Gates", 'M', 60};
    struct member person2={"Jobs", 'M', 55};
    struct member person3={"Jane", 'F', 45};    

    FILE *file;
    file = fopen("question3.dat","w");
    if(file == NULL)
        printf("question3.dat cannot be opened!\n");
    else
        printf("question3.dat was opened successfully.\n");

    fprintf(file, "%s %c %d\n", person1.lastName, person1.gender, person1.age);
    fprintf(file, "%s %c %d\n", person2.lastName, person2.gender, person2.age);
    fprintf(file, "%s %c %d\n", person3.lastName, person3.gender, person3.age);

    fclose(file);
} // end function populateDatFile

// begin function which reads a .dat file and propogates the array with the data in the .dat file
void readAndUpdate(struct member *person, size_t maxpeople){

    int i=0;

    FILE *file;
    file = fopen("question3.dat","r");
    if(file == NULL)
        printf("question3.dat cannot be opened!\n");
    else
        printf("question3.dat was opened successfully.\n");

    fscanf(file, "%s", &person->lastName);
    fscanf(file, " %c", &person->gender);
    fscanf(file, "%d", &person->age);

    fscanf(file, "%s", &person[1].lastName);
    fscanf(file, " %c", &person[1].gender);
    fscanf(file, "%d", &person[1].age);

    fscanf(file, "%s", &person[2].lastName);
    fscanf(file, " %c", &person[2].gender);
    fscanf(file, "%d", &person[2].age);

    fclose(file);

} // end function readAndUpdate

1 个答案:

答案 0 :(得分:2)

您拥有的代码是“确定,但是...”。还有一些相当重要的“ buts”值得担心。

第一个问题是您写的内容是否是您打算写的内容。您已经定义了一个指向结构的指针数组,但是根本没有初始化它。您可能打算定义一个结构数组而不是一个指针数组,这将改变其余的讨论。就目前而言,我认为您写的是“可以,这就是我要写的。”

您将数组正确传递给函数。但是,该函数不知道您传递的数组有多大。您应该养成告诉函数数组有多大的习惯。

您没有在函数内部引用数组。那还不全是坏事。您尚未定义数组中每个指针指向的内存。您大概会在添加项目时动态分配它们,然后使用箭头->而非点.正确引用它们:

void readAndUpdate(size_t max, struct member *people[max])
{
    for (size_t i = 0; i < max; i++)
    {
        people[i] = malloc(sizeof(*people[i]));
        if (people[i] == NULL)
            …handle error appropriately…
        strcpy(people[i]->lastName, "Unknown");
        people[i]->gender = 'N';   // Neuter — unknown
        people[i]->age = 0;        // Babies only
    }
}

int main(void)
{
    struct member *people[30] = { NULL }; 
    readAndUpdate(30, people);
    return 0;
}

如果条目数实际上不是固定的,则readAndUpdate()函数应报告已初始化的数量。


  

我不打算创建一个指针数组。

好;游戏规则就会改变:

void readAndUpdate(size_t max, struct member people[max])
{
    for (size_t i = 0; i < max; i++)
    {
        strcpy(people[i].lastName, "Unknown");
        people[i].gender = 'N';   // Neuter — unknown
        people[i].age = 0;        // Babies only
    }
}

int main(void)
{
    struct member people[30] = { { "", 0, 0 } }; 
    readAndUpdate(30, people);
    return 0;
}

已经分配了结构,并将其初始化为所有字节零。函数中的代码使用.而不是->来引用成员。 *来自变量和参数定义。