给数组指针分配空间malloc的问题

#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* 为了使用malloc(),free() */
#include <ctype.h>
#define YEARS 12

struct {
char (* name)[][];
char (* month)[][];
int days[12];
int mon_no[12];} season;

const char * ap[]={"JAY","FEB","MAR","API","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"}; /* 月份缩写 */
const char * app[]={"janoury","feburary","march","apirl","may","june","july","auguest","sepertember","octurber","novermber",
"decermber"}; /* 月份 */
int input();
int daycount(int n,int day[]); /* 根据输入找出月份,返回月份数字 */

int main()
{
int i,input;
int sum;
season.name=(char (*)[][])malloc(YEARS*sizeof(char *));
for(i=0;i<YEARS;i++)
(season.name)[i]=(char *)malloc(sizeof("sepertember")+1);
/* season.month=(char (*)[][])malloc(YEARS*4);
*/
for(i=0;i<YEARS;i++)
strcpy(season.name[i],app[i]); /* 各月英文名称 */
for(i=0;i<YEARS;i++)
strcpy(season.month[i],ap[i]); /* 每月英文简写 */
for(i=1;i<=7;i++) /* 存入每月天数 */
{
if(i==2)
seson.day[i-1]=28;
else if(i%2!=0)
season.days[i-1]=31;
else
season.days[i-1]=30;
}
for(i=8;i<=YEARS;i++)
{
if(i%2==0)
season.days[i-1]=31;
else
season.days[i-1]=30;
}
for(i=0;i<YEARS;i++) /* 各月数字表示 */
season.mon_no[i]=i+1;

input=input();
sum=daycount(input,season.days);
printf("%d\n",sum);
return 0;
}

int input()
{
int n;

puts("Enter an integer to show month:");
while(scanf("%d",&n)==0)
{
if(isalpha(getchar())
{
puts("Ur enter is a letter,plz re-enter.");
continue;
}
else
{
puts("whats the hell u input.");
continue;
}
}
while(n>12 || n<0)
{
puts("Ur enter is out of range.plz re-input:");
while(scanf("%d",&n)==0)
{
if(isalpha(getchar())
{
puts("Ur enter is a letter,plz re-enter.");
continue;
}
else
{
puts("whats the hell u input.");
continue;
}
}
}
return n;
}

int daycount(int n,int day[])
{
int i,sum=0;
for(i=0;i<n;i++)
sum+=day[i];
return sum;
}

如何分配结构中的2维函数的内存的问题。

这些教材玩具代码,谁有心思慢慢帮你看?

问题要聚焦。
温馨提示:答案为网友推荐,仅供参考
相似回答