#include <stdio.h>
int comparison(char str1[],char *str2)
{
int i;
for(i=0;i<10;i++)
{
if(str1 == '\0' && str2 == '\0')
{
return 1;
break;
}
else if (str1 != str2)
{
return 0;
break;
}
else
{
continue;
}
}
}
void main()
{
struct num
{
int nubers;
char *name;
};
struct num sum[4]={{0,"鱼"},{0,"仓"},{0,"吴"},{0,"苏"}};
char str[10];
int n,i;
int a[4] = {0,0,0,0}; //数组越界了,因为数组越界改变了i的值
int comparison(char str1[],char *str2);
for (n=1;n<4;n++)
{
printf("第%d位投票人:",n);
scanf("%s",str);
if(comparison(str,sum[0].name))
{
++sum[0].nubers;
}
else if (comparison(str,sum[1].name))
{
++sum[1].nubers;
}
else if (comparison(str,sum[2].name))
{
++sum[2].nubers;
}
else if (comparison(str,sum[3].name))
{
++sum[3].nubers;
}
else
{
continue;
}
}
for (i=0;i<4;i++)
{
a = sum.nubers;
}
printf("%s的得票数是:%d\n",sum[0].name,a[0]);
printf("%s的得票数是:%d\n",sum[1].name,a[1]);
printf("%s的得票数是:%d\n",sum[2].name,a[2]);
printf("%s的得票数是:%d\n",sum[3].name,a[3]);
}
高地址 | i | a[3] | a[2] | a[1] | a[0] | 低地址
数组越界了, 改变了i的值, 有的编译器做了优化了, 但是越界大了也会改变i的值
,6.0不知道优化了没。 把 int a[3]改成int a[4];
|