[英]how to pass a struct of array to function
我有一段代碼,我想變成一個函數,我不知道該怎么做,這段代碼從文件中讀取作者,然后將它們放入數組中。 我注意到主文件太大了,所以我必須簡化它。 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;
您首先需要聲明函數
void readAuthors()
或返回數組
struct readAuthors()
然后像往常一樣寫函數
void/struct readAuthors(){
<code>
}
之后,您可以調用該函數
readAuthors()
or
array = readAuthors
我希望這有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.