请问问大侠们,什么原因?
#include <stdio.h>#include <string.h>
#define MAX_LENGTH 60
#define END_OF_ARRAY '\0'
typedef struct week{
char name[MAX_LENGTH];
int number;
} Week;
int TestAllocSpace(int num, char **list)
{
if (num == 0)
*list = (char *)calloc(1, sizeof(Week));
else
*list = (char *)realloc(*list, sizeof(Week)*(num+1));
return(0);
}
int GetLine(FILE *fp, char string[MAX_LENGTH])
{
int len;
while (fgets(string, MAX_LENGTH, fp) != NULL)
{
len = strlen(string);
if (len > 1)
{
string[len-1] = END_OF_ARRAY;
return (0);
}
}
return (-1);
}
int main(int argc, char *argv[])
{
char line[MAX_LENGTH];
Week *list=NULL, *p;
int i, num=0;
FILE *fp;
if( argc < 2 )
exit(-1);
fp = fopen( argv[1], "r");
if( fp == NULL )
exit(-1);
while ( !GetLine(fp, line) )
{
TestAllocSpace(num, &list);
strcpy(list[num].name, line);
p = &(list[num]);
p->number = num;
num++;
}
for(i = num-1; i >= 0; i--)
printf("%d) %s\n", list[i].number, list[i].name);
free(list);
fclose(fp);
return(0);
}
该程序中子函数TestAllocSpace(int num, char **list)中第二个形参为char型,但主函数中调用该子函数时传递的参数为我week结构类型的,该程序可以运行成功,请问问大侠们,什么原因?