c - Problem with Array Input -


so here code:

#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 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 3..25\n");         exit(exit_failure);     } return scannedn; } void readboard(int board[max][max], int n) {     int i,j,nvr;      printf("enter %d %d forrest:\n",n,n);         for(i = 0; < 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 board:\n\n");         for(i = 0; < 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");         } } 

when compile it, completes after input array error:

*** 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 

can give me hand whats going on?

kind regards

dennis

you #define max n , int n; below that. so, n initialized 0 has file scope (someone correct me on if i'm wrong please, checked few examples that's no guarantee , don't have copy of standard handy). have in main:

int board[max][max]; 

and little bit later assign value n , proceed use that. but, board created using initial value of n. result assuming board bigger , that's how smash stack.


Comments