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$
3 Answers 3
Type of argv[3] and argv[4] is char*. Hence, the lines
int row = argv[3];
int col = argv[4];
are wrong. You are trying to initialize two int objects with char*.
If you run the program using:
program 10 20 30 40
then,
The value of argv[0] will be "program".
The value of argv[1] will be "10".
The value of argv[2] will be "20".
The value of argv[3] will be "30".
The value of argv[4] will be "40".
Yes, they seem like they are numbers in the command line but they are strings in a C program. You can use atoi to extract the integral numbers from the strings.
int row = atoi(argv[3]);
int col = atoi(argv[4]);
Make sure to use
#include <stdlib.h>
to get the declaration of the function.
Comments
When you write int row = argv[3]; you are making an int and initialising it to the value of argv[3]. However, argv is of type char **, which is a pointer to a pointer, or effectively an array of type char *. This is an invalid assignment, you need to make a char buffer to read the arguments in, then use atoi(argv[i]) to get the integer value.
Comments
the initially there was many errors with the code, i belive the following code is what it should have been..
#include <stdio.h>
#include <stdlib.h>
char **init_board(int row, int col);
int main(int argc, char** argv) {
int row = atoi(argv[3]);
int col = atoi(argv[4]);
char** board = init_board(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
board[i][j] = 'x';
printf("%c", board[i][j]);
}
}
return 0;
}
char **init_board(int row, int col) {
char** out = malloc(sizeof(char*) * row);
for (int i = 0; i < row; i++)
out[i] = malloc(sizeof(char) * col);
return out;
}
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.