Hallo, brauche Hilfe beim Erstellen einer multidimensionalen Array Ich bin neu bei C, jede Hilfe wird geschätzt. Dies ist der CodeErstellen eines multidimensionalen Arrays bei c
#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;
}
eine Idee, wie ich dieses Problem beheben kann? was mache ich falsch? wenn ich kompilieren erhalte ich folgende Warnungen und wenn ich es laufen, sagt Segmentation Fault
[email protected]:~/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;
^
[email protected]:~/s216/arc/ass1/ass1$ ./test x x 5 5
Segmentation fault
[email protected]:~/s216/arc/ass1/ass1$
'int row = argv [3] haben sollte;' linke Seite ist ein 'int ', rechte Seite ist ein' char * '. Das sind keine kompatiblen Typen - genau so, wie es dir die Warnung sagt. Sie müssen 'argv [3]' in ein int mit etwas wie 'atoi' oder besser noch' strtol' umwandeln. – kaylum
Vielleicht ist das völlig irrelevant, aber wenn ** alles fettgedruckt ist **, dann sticht nichts wirklich hervor. Formatierung soll die Aufmerksamkeit auf einen bestimmten Teil des Posts lenken, aber wenn du auf alles aufmerksam machst, hättest du genauso gut auf nichts aufmerksam machen können. – iRove