1

Im writing a function in c and here is my code:

char* makeMoves(char oldBoard[], int moveType, int empties, char player){
    int oldBoardLength;
    oldBoardLength = sizeof(oldBoard) / sizeof(oldBoard[0]);
    char result[oldBoardLength];
    copyBoard(oldBoard, result);
}

I think that this line has a problem:

char result[oldBoardLength];

how can i create this array with length=oldBoardLength? In java is something like this:

char[] result = new char[oldBoard.length];

but in c i don;t know how to create this. Can anyone help me?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
tath
  • 41
  • 1
  • 8
  • Please post the code where you call `makeMoves`. – Iharob Al Asimi May 06 '15 at 17:25
  • 2
    `sizeof(oldBoard) / sizeof(oldBoard[0])` isn't doing what you think it is. `oldBoard` is a pointer in this function; not an array. – WhozCraig May 06 '15 at 17:25
  • i have not finish the program yet, so i dont call the function makeMoves. I want to take the length of the array. i think that i have the full size of array(bit size) and i divide this with one cell of the array. So i get the length – tath May 06 '15 at 17:31

3 Answers3

1

In C, you have to allocate dynamic storage in such cases.

char *result = malloc(oldBoardLength);
copyBoard(oldBoard, result);
free(result);

However, you have to pass oldBoardLength into the function, because an argument like arr[] or arr[8] will always decay to a pointer. Taking sizeof on a pointer is not what you have intended. Have a look at the output of this example:

#include <stdio.h>

#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

long int test(char array[16]) {
        return COUNT_OF(array);
}

void main(void) {
        char a[16];
        printf("%ld\n", COUNT_OF(a));  // prints 16
        printf("%ld\n", test(a));      // prints 8 or 4 for 64bit or 32bit systems
}
user1978011
  • 3,419
  • 25
  • 38
0

First I would use char *oldBoard instead of char oldBoard[] There the same but I think char *oldBoard is clearer. Second you don't wan't to use sizeof as that will not return the correct length, you would just get the size of a pointer. sizeof(oldBoard) / sizeof(oldBoard[0]); only works on statically allocated arrays or at least that is what this says How do I find the length/number of items present for an array? . Use a another variable to keep track of the array length. Finally use dynamic allocation aka malloc() so that the values don't become garbage when you pass them between functions. I'm not quite sure what you are trying to do but here is a example of what I think your trying to do.

char *makeMoves(char *oldBoard, int len, int moveType, int empties, char player)
{
    char *result;

    result = malloc(len);
    if(result == NULL)
    {
        return NULL;
    }

    copyBoard(oldBoard, result);

    return result;
}

int main(void)
{

    char *board, *result;
    int len = 10;
    int moveType, empties;
    char player;

    board = malloc(len);
    if(board == NULL)
    {
        return -1;
    }

    result = makeMoves(board, len, moveType, empties, player);
    if(result == NULL)
    {
        return -1;
    }

    free(board);
    free(result);

    return 0;
}
Community
  • 1
  • 1
2trill2spill
  • 1,333
  • 2
  • 20
  • 41
0

In C, the most often used idiom is passing the expected number of elements your pointer parameter points to as a separate parameter. Should be something like this:

char* makeMoves(char *oldBoard, int oldBoardLength, int moveType, int empties, char player) {
    /* ... */
}

This way, the caller of your function is repsonsible for passing in the correct length.