阵列输入问题

时间:2011-04-29 04:27:20

标签: c arrays

所以这是我的代码:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX n
int t,n;
int readtime ();
int readboardsize();
void readboard(int board[MAX][MAX], int n);
void printboard(int board[MAX][MAX]);

int main(int argc, char * argv[]){
    int board[MAX][MAX]; 
    t = readtime();
    n = readboardsize();
    printf("%d\n",n);
    readboard(board,n);
    printboard(board);
 return 0;

}

int readtime() {
    int nvr, scannedt;

    printf("Enter t :");
    nvr = scanf("%d",&scannedt);
    if (scannedt>1000 || scannedt<0 || nvr==0) {
        printf("Incorrect input: t must be 0..1000\n");
        exit(EXIT_FAILURE);
    }    
return scannedt;
} 

int readboardsize() {
    int nvr,scannedn;
    printf("Enter n :");
    nvr = scanf("%d",&scannedn);
    if (scannedn>25 || scannedn<3) {
        printf("Incorrect input: n must be 3..25\n");
        exit(EXIT_FAILURE);
    }
return scannedn;
}
void readboard(int board[MAX][MAX], int n) {
    int i,j,nvr;

    printf("Enter %d by %d forrest:\n",n,n);
        for(i = 0; i < MAX; i++){
            for(j=0; j < MAX; j++){
                nvr = scanf("%d",&board[i][j]);      
            }
        }
}              
void printboard(int board[MAX][MAX]) {
    int i,j;
    printf("Here is the board:\n\n");
        for(i = 0; i < MAX; i++){
            for(j=0; j < MAX; j++){
                if (board[i][j]==0 ) {
                    printf(". ");
                }else if (board[i][j]==1){
                    printf("^ ");
                }else if (board[i][j]==2){
                    printf("* ");
                } 
            }      
        printf("\n");
        }
}

当我编译它时,它成功完成但在我输入数组后我得到了这个错误:

*** stack smashing detected ***: ./ass2 terminated
======= Backtrace: =========
/lib/libc.so.6(__fortify_fail+0x50)[0x3e3990]
/lib/libc.so.6(+0xe593a)[0x3e393a]
./ass2[0x8048768]
/lib/libc.so.6(__libc_start_main+0xe7)[0x314ce7]
./ass2[0x8048421]
======= Memory map: ========
00110000-0012a000 r-xp 00000000 08:01 129968     /lib/libgcc_s.so.1
0012a000-0012b000 r--p 00019000 08:01 129968     /lib/libgcc_s.so.1
0012b000-0012c000 rw-p 0001a000 08:01 129968     /lib/libgcc_s.so.1
0025e000-0025f000 r-xp 00000000 00:00 0          [vdso]
002fe000-00455000 r-xp 00000000 08:01 134635     /lib/libc-2.12.1.so
00455000-00456000 ---p 00157000 08:01 134635     /lib/libc-2.12.1.so
00456000-00458000 r--p 00157000 08:01 134635     /lib/libc-2.12.1.so
00458000-00459000 rw-p 00159000 08:01 134635     /lib/libc-2.12.1.so
00459000-0045c000 rw-p 00000000 00:00 0 
00c1f000-00c3b000 r-xp 00000000 08:01 130134     /lib/ld-2.12.1.so
00c3b000-00c3c000 r--p 0001b000 08:01 130134     /lib/ld-2.12.1.so
00c3c000-00c3d000 rw-p 0001c000 08:01 130134     /lib/ld-2.12.1.so
08048000-08049000 r-xp 00000000 08:01 427835     /home/stu/Work/comp1911/ass2/ass2
08049000-0804a000 r--p 00000000 08:01 427835     /home/stu/Work/comp1911/ass2/ass2
0804a000-0804b000 rw-p 00001000 08:01 427835     /home/stu/Work/comp1911/ass2/ass2
09c12000-09c33000 rw-p 00000000 00:00 0          [heap]
b7830000-b7831000 rw-p 00000000 00:00 0 
b783e000-b7842000 rw-p 00000000 00:00 0 
bfe46000-bfe67000 rw-p 00000000 00:00 0          [stack]
Aborted

任何人都可以帮我一试吗?

亲切的问候

丹尼斯

1 个答案:

答案 0 :(得分:1)

#define MAX n然后int n;就在那之下。所以,n将被初始化为零,因为它有文件范围(如果我错了请有人纠正我,我查了几个例子,但这不能保证,我没有标准的副本便利)。然后你有这个主要的:

int board[MAX][MAX];

然后稍后您最终为n分配一个值,然后继续使用它。但是,您的board是使用n的初始值创建的。结果是一切都假设board比实际大,这就是你粉碎堆栈的方式。