I am trying to pass an uninitialized 2-D char array that is declared in main and define it in the function.
I am receiving a segmentation fault and the debugger says:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004005af in initialize_world (array=0x7fffffffdff0, maxRows=6,
maxCols=5) at ec.c:33
33 array[i][j]='-';
Here is the code for the prototype, main and the function:
#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]='-';
}
}
}
A banality, you’re using i instead of j:
Typo error, you should have written j++, that’s why you overflow.