Hello, need help creating a multi dimensional array i am new at C, any help is appreciated. this is the code
#include <stdio.h>
char init_board(int row, int col);
int main(int argc, char** argv) {
int row = argv[3];
int col = argv[4];
char** board = init_board(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf("%d", board[i][j]);
}
}
return 0;
}
char init_board(int row, int col) {
int count = 0;
int count2 = 0;
char** out;
while (count < row) {
while (count2 < col) {
out[count][count2] = ".";
count2++;
}
count++;
count2 = 0;
}
return out;
}
any idea how i can fix this? what i am doing wrong? when i compile i receive following warnings and when i run it says segmentation fault
s@ss:~/s216/arc/ass1/ass1$ gcc ass1.c -std=c99 -o test
ass1.c: In function ‘main’:
ass1.c:6:12: warning: initialization makes integer from pointer without a cast [enabled by default]
int row = argv[3];
^
ass1.c:7:12: warning: initialization makes integer from pointer without a cast [enabled by default]
int col = argv[4];
^
ass1.c:8:17: warning: initialization makes pointer from integer without a cast [enabled by default]
char** board = init_board(row, col);
^
ass1.c: In function ‘init_board’:
ass1.c:27:23: warning: assignment makes integer from pointer without a cast [enabled by default]
out[count][count2] = ".";
^
ass1.c:33:2: warning: return makes integer from pointer without a cast [enabled by default]
return out;
^
s@ss:~/s216/arc/ass1/ass1$ ./test x x 5 5
Segmentation fault
s@ss:~/s216/arc/ass1/ass1$
int row = argv[3];Left hand side is anint, right hand side is achar *. That are not compatible types - exactly as the warning tells you. You need to convertargv[3]to an int with something likeatoior better stillstrtol.