新手求教,这段代码为什么能通过编译但一运行就崩溃? 财富值4

2016-10-16 02:46发布

#include <stdio.h>

#define NUM 100

int max(int grade[], int n);
int min(int grade[], int n);
int main(void)
{

int i; int j; int n, x, y; char c; char code[n][NUM]; char name[n][NUM]; int grade[i]; scanf("%d", &n); for (i = 0; i != n; i++) {     for (j = 0; ((int)(c = getchar())) != 32; j++) {         name[i][j] = c;     }     for (j = 0; ((int)(c = getchar())) != 32; j++) {         code[i][j] = c;     }      scanf("%d", &grade[i]); } x = max(grade, n); y = min(grade, n); printf("%s %s
%s %s
", name[x], code[x],name[y], code[y]); return 0;

}

int max(int *grade, int const n)
{

int tmp; tmp = grade[0]; int i = 1; int j = 1; while (i < n) {     if (grade[i]  > tmp){         tmp = grade[j];         i ++;         j ++;     }     else {     i ++;     j ++;     } } return j;

}

int min(int *grade, int const n)
{

int tmp; tmp = grade[0]; int i = 1; int j = 1; while (i < n) {     if (grade[i]  < tmp){         tmp = grade[j];         i++;         j++; }     else {     i ++;     j ++; } } return j;

}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
4条回答

max/min返回的int一定比grade数组的size大,然后printf会做成非法内存访问,依执行环境不同,可能崩溃或打印垃圾

一周热门 更多>