-3

Edit3: Added Example.

Edit2: So, because I am new to coding in general and because of the current comments,I need to ask, would it be better if I posted the entire code as a comment? (around 90 lines)

So, I have been playing around with dynamic memory allocation and I have a 2D board, witch fills with '.'. Then I import the board in a function, witch checks for available cells (cell with '.'==available). It compiles ok, but when I run it I get

segmentation fault

Here's the code for the board malloc

**board = (char**) malloc(x_input*sizeof(char*));
for(i = 0; i <x_input; i++){
    board[i] = (char*) malloc(y_input*sizeof(char));
}

for (i = 0; i<x_input; i++){
    for(j = 0; j<y_input; j++){
        board[i][j]='.';
    }
}

Here's the Function

int checker(int x_axis, int y_axis, char **board){         
    if (board[x_axis][y_axis] == '.'){
        return 1;
    } else { 
        return 2; 
    }
}

And here's the only time(so far) that I call the function Edit: x_replacement and y_replacement are assigned random values through a rand function

do{
    board[x_replacement][y_replacement] = '$';  
} while(checker(x_replacement, y_replacement, board) == 2);

EX:

  const int MAX_X = 40;
  const int MAX_Y = 40;
  const int MIN_X = 20;
  const int MIN_Y = 20;
  int x_input, y_input;

  int main(void){
    char **board;
    int i, j, k, obstacles, enemies, choice;

    do{
        printf("Enter board size. (Must be between (%d, %d) and (%d, %d))\n:", MIN_X, MIN_Y, MAX_X, MAX_Y);
        scanf("%d%d", &x_input, &y_input);
    }while ((x_input <= MIN_X && x_input >= MAX_X) && (y_input <= MIN_Y && y_input >= MAX_Y));

   *board = malloc(sizeof(char[x_input][y_input]));
    assert(*board != NULL);

    for (i = 0; i<x_input; i++){
        for(j = 0; j<y_input; j++){
            board[i][j]='.';
        }
    }
    return 0;
  }     
Alex
  • 53
  • 8
Lucifer__
  • 1
  • 2
  • 1
    Please [edit] our question to show us a [mcve]. When and where do you have the first code-snippet? When and where and with what arguments do you call the `checker` function? And as a newcomer here please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 21 '20 at 05:32
  • 1
    While not forgetting the minimal part of your [mcve], making it reproducible is also rather important. How are the first and last code snippets related? How are they placed in relation to each other? What are the initial values for `x_replacement` and `y_replacement`? – Some programmer dude Apr 21 '20 at 05:37
  • 2
    Hint: `**board = (char**) malloc(x_input*sizeof(char*));` in terms of an equation of types is `char = char**`. See anything wrong with that? It might not be your only problem, but it is definitely the first thing you should fix since `char` likely has a range up to 127 or 255 on your machine while `char**` (or any pointer) has a range up to 4294967295 or higher. – MemReflect Apr 21 '20 at 05:41
  • `**board = (char**) ...` will not compile cleanly on a standard C compiler. So if it did even without warnings, malloc is the least of your problems. – Lundin Apr 21 '20 at 06:47
  • Update: Can someone explain to me why `board = (char**)...` does not work, since this is the way to malloc int arrays (or so I am taught in uni rn) – Lucifer__ Apr 21 '20 at 07:00
  • Probably some syntax error since you don't seem to post the actual code. But regardless of that, this isn't the correct way to allocate 2D int arrays either. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Apr 21 '20 at 08:06
  • Depending on the type of `board` (which you don't actually show us) then `board = malloc(...)` might work fine. But `**board = malloc(...)` should not. Please let us stop guessing, and show us a proper [mcve]. – Some programmer dude Apr 21 '20 at 08:41
  • Oh and also please read [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Some programmer dude Apr 21 '20 at 08:42
  • Your new code is better, but doesn't match what you initially told us (the first code snippet). It also won't work since you dereference the uninitialized pointer `board`. – Some programmer dude Apr 21 '20 at 12:33

1 Answers1

0

I approached this from a different perspective, thanks to Ludin's and Some Programmer Dude's links so the result, witch works is the following:

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

void arr_alloc (size_t x, size_t y, char(**board)[x][y])
{
  *board= malloc( sizeof(char[x][y]) ); 
  assert(*board!= NULL);
}

void arr_fill (size_t x, size_t y, char board[x][y])
{
  for(size_t i=0; i<x; i++)
  {
    for(size_t j=0; j<y; j++)
    {
      array[i][j] = '.';
    }
  }
}

void arr_print (size_t x, size_t y, char board[x][y])
{
  for(size_t i=0; i<x; i++)
  {
    for(size_t j=0; j<y; j++)
    {
      printf("%c ", board[i][j]);
    }
    printf("\n");
  }
}

int main (void)
{
    int x,y;
    char (*board)[x][y];
    printf("enter dimentions: \n");
    scanf("%d%d", &x, &y);



  arr_alloc(x, y, &board);
  arr_fill(x, y, *board);
  arr_print(x, y, *board);
  free(board); 

  return 0;
}
Lucifer__
  • 1
  • 2