C 如何将数组的结构传递给函数

C 如何将数组的结构传递给函数,c,arrays,structure,C,Arrays,Structure,我有一段代码,我想把它变成一个函数,我不知道怎么做,这段代码从文件中读取作者,并将他们放入一个数组中。我注意到主文件变得太大了,所以我必须简化它。nartisti是文件中实际包含的艺术家数量 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <time.h> #define max 30 #

我有一段代码,我想把它变成一个函数,我不知道怎么做,这段代码从文件中读取作者,并将他们放入一个数组中。我注意到主文件变得太大了,所以我必须简化它。nartisti是文件中实际包含的艺术家数量

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define max 30            
#define maxu 20            
#define sessomu 6          
#define sessom 6          

struct cantanti
{
int id;
char nome[max],cognome[max],nazionalita[max],sesso[sessom],genere[max];
};
struct cantanti arrayc[100];

 case 1:
    {
        system("cls");//pulisco lo schermo

        FILE *fp;//puntatore a file
        int nartisti;//variabile per contare quanti artisti ci sono nel file
        fp=fopen("artisti.dat","r");//apro il file artisti in modalita' lettura
        fscanf(fp,"%d",&nartisti);//leggo da file il numero degli artisti presenti
        int i=0;//azzero la variabile per ciclare il contatore
        printf("  _____________________________________________________________________________________\n");
        printf("|| %-5s || %-12s || %-12s || %-15s || %-4s || %-12s ||\n","<ID>","<NOME>","<COGNOME>","<NAZIONALITA'>","<SESSO>","<GENERE>");
        printf("||_______||______________||______________||_________________||_________||______________||\n");

        while(i!=nartisti)
        {
            //scansioni gli artisti da file, gli inserisco in un array di cantanti e li visualizzo a schermo
            fscanf(fp,"%d %s %s %s %s %s",&arrayc[i].id,arrayc[i].nome,arrayc[i].cognome,arrayc[i].nazionalita,arrayc[i].sesso,arrayc[i].genere);
            printf("||%-6d || %-12s || %-12s || %-15s || %-7s || %-12s ||\n",arrayc[i].id,arrayc[i].nome,arrayc[i].cognome,arrayc[i].nazionalita,arrayc[i].sesso,arrayc[i].genere);
            printf("||_______||______________||______________||_________________||_________||______________||\n");
            i++ ;
        }
        //mi dice quanti cantanti sono stati letti
        printf("**************************|    Sono stati letti %d autori dal file      |****************\n",nartisti);
        printf("***************************\\___________________________________________/ ****************\n\n");
        fclose(fp);//chiudo il file

        system("pause");
        system("cls");//pulisce lo schermo
    }
    break;
#包括
#包括
#包括
#包括
#包括
#定义最大值30
#定义maxu 20
#定义sessomu 6
#定义sessom 6
坎坦蒂结构
{
int-id;
char nome[max]、cognome[max]、nazionalita[max]、sesso[sessom]、genere[max];
};
结构cantanti arrayc[100];
案例1:
{
系统(“cls”);//pulisco lo schermo
FILE*fp;//puntatore一个文件
int nartisti;//每个contare quanti artisti ci sono nel文件的变量
fp=fopen(“artisti.dat”,“r”);//在modalita'lettura中的一个文件artisti
fscanf(fp、%d、&nartisti);//leggo da文件的编号
int i=0;//每个循环的变量为零
printf(“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;

printf(“| |%-5s | |%-12s | |%-12s | |%-15s | |%-4s | |%-12s | | |”、“”、“”,“首先需要声明函数

void readAuthors()
readAuthors()
or
array = readAuthors
或者返回数组

struct readAuthors()
然后,只需按正常方式编写函数

void/struct readAuthors(){ 
    <code>
}
之后,您可以调用该函数

void readAuthors()
readAuthors()
or
array = readAuthors

我希望这有助于

函数在代码中的位置?我希望您能帮助我编写它,据我所知,我应该只传递数组和文件,但我不能很好地完成它。首先,您应该决定在函数中需要什么代码,因为您已经开始使用
case
语句,并且代码也正在打印出来e数组也是-如果它是一个用于读取的函数,它应该就是这样做的。你不能在C.void readAuthors()中返回数组-我应该在main中的main-struct readAuthors()之后的主void/struct readAuthors()之前写入数组?
struct readAuthors()
缺少结构的名称,只返回其中一个