我試圖傳遞一個未初始化的2-D char數組,它在main中聲明並在函數中定義它。將未初始化的2D Char數組傳遞到函數中
我收到分段錯誤和調試器說:
Program received signal SIGSEGV, Segmentation fault. 0x00000000004005af in initialize_world (array=0x7fffffffdff0, maxRows=6, maxCols=5) at ec.c:33 33 array[i][j]='-';
這裏是爲原型,主函數的代碼:
#include <stdio.h>
#include <string.h>
#define ROWS 10
#define COLS 12
void initialize_world(char array[][COLS],int maxRows,int maxCols);
int main(int argc, char *argv[]) {
char array[ROWS][COLS];
int numOfRows, numOfCols;
numOfRows = 6;
numOfCols = 5;
initialize_world(array,numOfRows,numOfCols);
return 0;
}
void initialize_world(char array[][COLS],int maxRows,int maxCols) {
int i,j;
for(i=0; i < maxRows;i++) {
for(j=0; j < maxCols;i++) {
array[i][j]='-';
}
}
}
爲什麼你有ROWS-numOfRows和COLS-numOfCols? – adripanico