2

So my array is :

int **board = (int **)malloc(size * sizeof(int *)); //declaring board
for (int i  = 0; i < size; i++)
    board[i] = (int *)malloc(size * sizeof(int));

and my show score function is this

void showScore(int **arr)
{
    int score = 0;

    for(int i = 0;  i <size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            score += arr[i][j];
        }
    }
    printf("score %d\n\n" , score - 2);


}

the problem I get is that the function does not work when I call it like

 showScore(&board);

any suggestions how to fix my function/call it? All I need is it to calculate the sum of a 2 d dynamic array

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Arn as
  • 23
  • 2
  • 1
    Welcome to Stack Overflow! [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 12 '16 at 18:00
  • 2
    Hint: `&board` != `int **`. – Sourav Ghosh Dec 12 '16 at 18:01
  • showScore(board); does not work either :/ – Arn as Dec 12 '16 at 18:12
  • 1
    You mind creating a [___MCVE___](http://stackoverflow.com/help/mcve)? – Sourav Ghosh Dec 12 '16 at 18:15
  • Heh (: , fenks by writing it I found my mistake, looks like in one part of the code I used a function of arr[size][size]; and somehow it messed up my array , changed the function to **arr and it works perfect. btw now I pass function(board) – Arn as Dec 12 '16 at 18:55

1 Answers1

1

You are passing the address of a pointer to a function that takes a pointer as a parameter. The 'board' variable contains the address of the first element in your array (e.g., board == &board[0][0]). You should simply pass 'board' to your function, as it is of type pointer (as your function takes as a parameter), rather than pass the address of the part of data that stores this pointer's value (as you currently do).

awerchniak
  • 357
  • 3
  • 16