我正在尝试分配一个结构数组。当我尝试初始化一个值并在函数中打印该值时,一切正常。当我在 main 中打印相同的值时,我的程序崩溃了。
void getHeader(FILE* fpFile, HEADER *pHead)
{
// Local Declaration
int worker, salesWeek, i;
PERSON *list;
// Statement
fscanf(fpFile, "%d %d", &worker, &salesWeek);
list = aloPerson(worker);
HEADER header = {worker, salesWeek, list};
pHead = &header;
return;
}// getHeader
PERSON* aloPerson(int worker)
{
// Local Declaration
PERSON *list;
// Statement
list =(PERSON*)calloc(worker, sizeof(PERSON));
if(list == NULL)
{
MEM_ERROR, exit(103);
}
return list;
}// aloPerson
这是我的结构。
typedef struct
{
int worker;
int weeks;
PERSON *pAry;
}HEADER;
这是主要的:
int main ( void )
{
// Local Declaration
FILE* fpFile;
char nameIn[25];
char *endPro = "end";
HEADER *pHead;
// Statement
printf("Please select file to to open.\nsales or sales_2: ");
scanf("%s", nameIn);
FLUSH;
do
{
valiFile(nameIn);
fpFile = openFile(nameIn);
getHeader(fpFile, pHead);
readFile(fpFile, pHead);
repeat(nameIn);
}
while(strcmp(nameIn, endPro) != 0);
return 0;