Cornul11 Cornul11 - 3 months ago 17
C Question

Segmentation fault on passing arguments to main

I know this is kinda basics, but i'm stuck at this.
I've been trying to make a sudoku game. The arguments would by given by passing them to the main. When trying to write them in a different array, than the **argv one, it is giving me a segmentation fault when increment an integer variable.

int **ft_copy_sudoku(int argc, char **argv)
{
int **sudoku_arr;
int index;
int s_index;
int j;

printf("%d", argc);
sudoku_arr = (int **)malloc(sizeof(int) * 9 * 9);
index = 1;
while (index < argc)
{
j = 0;
s_index = 0;
//sudoku_array[s_index] = (int *)malloc(sizeof(int) * 9);
while (j < 9)
{
if (argv[index][j] >= '1' && argv[index][j] <= '9')
sudoku_arr[s_index][j] = argv[index][j] - '0';
else
argv[index][j] = 0;
j++;
s_index++;
}
index++;
}
return sudoku_arr;
}
void ft_print_sudoku(int **sudoku)
{
int i;
int j;

i = 0;
while (i < 9)
{
j = 0;
while (j < 9)
{
printf("%d ", sudoku[i][j]);
j++;
}
i++;
printf("\n");
}
}
int main(int argc, char **argv)
{
ft_print_sudoku(ft_copy_sudoku(argc, argv));
return (0);
}


When debugging with the gdb, i got the following message.

Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x000000000004007a6 in ft_print_sudoku (sudoku=0x1060420) at puzzle.c:62
62 j++;

Answer

Going on the assumption that your command line parameters are sequences of digits+markers in sets of nine chars per (and you have up to nine of these) It appears you're trying to allocate a dynamic table of 9x9 int values to store those digits.

Your code as last-posted is using incorrect indirection. You're declaring an int**, setting it to malloc(9*9*sizeof(int)), then treating it as an array of pointers, which it is not.

If you want to use a pointer to an array of 9-int, then use an appropriate pointer-to-type to reflect that. Such a pointer looks like this:

int (*ptr)[9];

Using that, you can craft your function to allocate a sequence of 9 elements of that pointed-to type:

int (*arr)[DIM] = calloc(DIM, sizeof *arr);

where DIM is the table dimension you're allocating, known at compile-time, in this case 9. The last oddity is the method for returning such a thing from your function, which can be done any number of ways. One way, though having a infrequently used syntax, looks like this:

int (*ft_copy_sudoku(int argc, char **argv))[DIM]
{
    int (*arr)[DIM] = calloc(DIM, sizeof *arr);

    .... code ....

    return arr;
}

Another arguably clearer way uses a typedef for the pointer-to-array-of-DIM-int :

typedef int (*row_ptr)[DIM];

row_ptr ft_copy_sudoku(int argc, char **argv)
{
    row_ptr arr = calloc(DIM, sizeof *arr);

    .... code ....

    return arr;
}

Putting It Together

Taking in all of that, the result looks something like this:

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

#define DIM  9

int (*ft_copy_sudoku(int argc, char **argv))[DIM]
{
    int (*arr)[DIM] = calloc(DIM, sizeof *arr);
    int ridx, cidx;

    for (ridx=1; ridx < argc && ridx <= DIM; ++ridx)
    {
        char *row = argv[ridx];
        for (cidx=0; *row && cidx < DIM; ++cidx, ++row)
        {
            if (*row >= '1' && *row <= '9')
                arr[ridx-1][cidx] = *row - '0';
        }
    }
    return arr;
}


void ft_print_sudoku(int (* const arr)[DIM])
{
    int i,j;
    for (i=0; i<DIM; ++i)
    {
        for (j=0; j<DIM; ++j)
            printf("%d ", arr[i][j]);
        putc('\n', stdout);
    }
}


int main(int argc, char *argv[])
{
    int (*sudoku)[DIM] = ft_copy_sudoku(argc, argv);
    ft_print_sudoku(sudoku);
    free(sudoku);
    return 0;
}

An example of this with a stack of 9 command line args of digits and markers can be seen here. The example command line with arguments and printed output are presented below:

./a.out 123-567-9 234-67891 3-5678-1- ---789123 5-7-9-2-4 67-9123-5 78-12345- 8-123-567 -1-3-5-7-

1 2 3 0 5 6 7 0 9 
2 3 4 0 6 7 8 9 1 
3 0 5 6 7 8 0 1 0 
0 0 0 7 8 9 1 2 3 
5 0 7 0 9 0 2 0 4 
6 7 0 9 1 2 3 0 5 
7 8 0 1 2 3 4 5 0 
8 0 1 2 3 0 5 6 7 
0 1 0 3 0 5 0 7 0 
Comments