作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
typedef struct
{
char janeiro[31];
char *fevereiro;
char marco[31];
char abril[30];
char maio[31];
char junho[30];
char julho[31];
char agosto[31];
char setembro[30];
char outubro[31];
char novembro[30];
char dezembro[31];
int mesSize[12] = { 31, 28, 31, 30, 31 , 30, 31, 31, 30 , 31, 30, 31 };
char *meses[12]={ janeiro, fevereiro, marco, abril, maio, junho, julho, agosto, setembro, outubro, novembro, dezembro };
} MESES;
int n_diasK(MESES *mes)
{
int i, j, counter=0;
for (i = 0; i < 12; i++)
{
for(j = 0; j < (mes->mesSize[i]) ; j++){
if (i == 1)
{
if (mes->fevereiro[j] == 'K');
counter++;
}
else if (mes->meses[i][j] == 'K' )
counter++;
}
}
return counter;
}
void menu()
{
MESES mes;
randLetters(year,&mes);
n_diasK(&mes);
}
好的,这是真实程序的相关部分,基本上程序的作用是要求用户选择年份,然后在 randLetters() 函数中为每个月的每一天分配一个随机字母,然后它必须找出字母“K”有多少天。在结构中 char *fevereiro(february) 是一个指针而不是数组,因为我需要计算闰年。我上面发布的代码可以工作,但我必须包含二月的特殊情况 if(mes->fevereiro[j] == 'K')
因为如果在没有此检查的情况下运行 for 循环,它会在到达 mes->meses[1][j] == 'K'
时崩溃。 .
最佳答案
我相信问题在于这个分配:
char *2Darray[2]={somearray, somepointer};
在 somepointer
的整个生命周期内,其行为与您期望的不同。
也就是说,当您分配内存,然后让 somepointer
指向该内存时,2Darray[1]
仍然是指向其他地方的 char* 。无论 somepointer
在结构初始化时持有/指向什么值,都将是 2Darray[1]
中的值。
关于c - 为什么我必须在 for 循环中跳过二月并使用 mes->fevereiro[j] 而不是 mes->meses[1][j]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41352854/
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我是一名优秀的程序员,十分优秀!