0

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$
asked Aug 3, 2016 at 4:56
2
  • int row = argv[3]; Left hand side is an int, right hand side is a char *. That are not compatible types - exactly as the warning tells you. You need to convert argv[3] to an int with something like atoi or better still strtol. Commented Aug 3, 2016 at 5:00
  • 1
    Maybe this is completely irrelevant, but if everything is in bold, then nothing really stands out. Formatting is meant to draw attention to a specific part of the post, but if you draw attention everything, you may as well have drawn attention to nothing. Commented Aug 3, 2016 at 5:07

3 Answers 3

2

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.

answered Aug 3, 2016 at 5:20
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Aug 3, 2016 at 5:09

Comments

0

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;
}
answered Dec 3, 2017 at 15:40

1 Comment

Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.