1

In my C program, I use a void function with the following arguments: One 2D int array, one int pointer that will be used to create the new dynamic array and a last int pointer which will hold a number of counts that will occur inside the function. So the dynamic array is created in the function using malloc and everything works okay, until I print its elements in main() after calling the function. What I get is rubbish instead of the numbers I should see. Here's the function code:

void availableMoves(int array[][3], int *av, int *counter)
{
    int i, j;
    for (i=0; i<3; i++)
    {
        for (j=0; j<3; j++)
        {
            if (array[i][j] == E)
            {
                printf("%d ", 3*i + j + 1);
                (*counter)++;
            }
        }
    }
    av = (int *) malloc(*counter * sizeof(int));
    if (av == NULL)
    {
        printf("ERROR!");
    }
    else
    {
        for (i=0; i<*counter; i++)
            *(av + i) = 0;
        int pos = 0;
        for (i=0; i<3; i++)
        {
            for (j=0; j<3; j++)
            {
                if (array[i][j] == E)
                {
                    *(av + pos++) = 3*i + j + 1;
                }
            }
        }
    }
}
5
  • Just commenting on code: Since you have an error case, you should not modify *counter like that in error case. Use a temp int tmp_count = *counter; variable, and assign it back to *counter only if function succeeded. Alternatively, make it abort(); if malloc fails, or something. Avoid producing "partial" result (av=null but *counter is still modified). Commented Jan 2, 2013 at 11:24
  • Try to understand pointer-dereference and indexing. *(av + pos++) = 3*i + j + 1; is the same as av[pos++] = 3*i + j + 1;, but most human readers prefer the second form. Similar for (*counter)++; which could be written as *counter += 1;, avoiding the parentheses. Commented Jan 2, 2013 at 11:24
  • @hyde: You're right, but that was a quick check of malloc, I'm not done with it yet. :) Commented Jan 2, 2013 at 14:11
  • @wildplasser: Is there something wrong with the parentheses? Commented Jan 2, 2013 at 14:11
  • No, there is nothing wrong with them. But most people tend to reduce the number of parentheses, just because it is easier to read with fewer ((.)(.)). Commented Jan 2, 2013 at 14:15

2 Answers 2

2

In this function, av is a pointer passed by copy. So when you change the value of your pointer inside the function, the original pointer won't be modified.

There are two possibilities :

  • use a pointer to pointer (int **av);
  • return the allocated pointer (return av).

So either:

void availableMoves(int array[][3], int **av, int *counter);

Or:

int *availableMoves(int array[][3], int *av, int *counter)

And the call:

availableMoves(array, &av, &counter);
av = availableMoves(array, av, &counter);
Sign up to request clarification or add additional context in comments.

3 Comments

Third (IMO most consistent) approach would be to return boolean value: return true when both av and counter have been successfully modified, otherwise return false and leave av and counter unmodified.
@hyde: Could be indeed a good idea to use the return value otherwise.
Decided to use the first approach and it seems to be working! Thank you!
2

use double pointer for your dynamic array int **av instead of int *av

void availableMoves(int array[][3], int **av, int *counter)

and into the function change av by *av

Comments

Your Answer

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.