如何使用c中的参数声明数组大小

时间:2021-07-20 01:21:08

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

我在c中写了一个函数,这是我的代码:

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:

如何用length = oldBoardLength创建这个数组?在java中是这样的:

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

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

但是我不知道如何创造这个。谁能帮我?

3 个解决方案

#1


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

在C中,您必须在这种情况下分配动态存储。

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:

但是,您必须将oldBoardLength传递给函数,因为像arr []或arr [8]这样的参数将始终衰减为指针。在指针上取大小并不是你想要的。看看这个例子的输出:

#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
}

#2


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 * oldBoard而不是char oldBoard []但是我认为char * oldBoard更清晰。其次,你不要使用sizeof,因为它不会返回正确的长度,你只需要获得指针的大小。 sizeof(oldBoard)/ sizeof(oldBoard [0]);仅适用于静态分配的数组或至少这就是说如何找到数组的项目长度/数量? 。使用另一个变量来跟踪数组长度。最后使用动态分配aka malloc(),以便在函数之间传递值时,这些值不会变为垃圾。我不太确定你想要做什么,但这里是我认为你想要做的一个例子。

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;
}

#3


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:

在C中,最常用的习惯是将指针参数指向的预期元素数作为单独的参数传递。应该是这样的:

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.

这样,函数的调用者可以传递正确的长度。

#1


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

在C中,您必须在这种情况下分配动态存储。

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:

但是,您必须将oldBoardLength传递给函数,因为像arr []或arr [8]这样的参数将始终衰减为指针。在指针上取大小并不是你想要的。看看这个例子的输出:

#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
}

#2


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 * oldBoard而不是char oldBoard []但是我认为char * oldBoard更清晰。其次,你不要使用sizeof,因为它不会返回正确的长度,你只需要获得指针的大小。 sizeof(oldBoard)/ sizeof(oldBoard [0]);仅适用于静态分配的数组或至少这就是说如何找到数组的项目长度/数量? 。使用另一个变量来跟踪数组长度。最后使用动态分配aka malloc(),以便在函数之间传递值时,这些值不会变为垃圾。我不太确定你想要做什么,但这里是我认为你想要做的一个例子。

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;
}

#3


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:

在C中,最常用的习惯是将指针参数指向的预期元素数作为单独的参数传递。应该是这样的:

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.

这样,函数的调用者可以传递正确的长度。