函数参数作为具有声明大小的数组

时间:2021-05-16 19:34:33

I frequently use the following convention to inform client code that a function requires an argument of an array with defined size:

我经常使用以下约定来通知客户端代码函数需要具有已定义大小的数组的参数:

/* File foo.h */
int foo (int arg[10]);

The message I want to give to client code is that they must provide an array of type int with 10 positions.

我想给客户端代码的消息是它们必须提供一个int类型的数组,其中包含10个位置。

I am aware that it is not very usual, so I came here to ask: Am I missing any side effect of this convention ? Is it anyhow harmful?

我知道这不常见,所以我来到这里问:我是否遗漏了这个惯例的任何副作用?无论如何有害吗?

Thank!

3 个解决方案

#1


2  

If you want to insist on getting an array of size 10, you can use:

如果你想坚持获得一个10号的数组,你可以使用:

int foo (int (*arg)[10]);

The ill-side effects of this are:

这种不良影响是:

  1. In the function, you have to use:

    在函数中,您必须使用:

    (*arg)[index] 
    

    instead of just

    而不仅仅是

    arg[index]
    
  2. The calling function must use:

    调用函数必须使用:

    int array[10];
    foo(&array);
    

    instead of

    int array[10];
    foo(array);
    
  3. You cannot use an array that has more than 10 elements.

    您不能使用包含10个以上元素的数组。

    int array[20];
    foo(&array);   // Not OK.
    
  4. You cannot use a malloced array.

    您不能使用malloced数组。

    int* array = malloc(sizeof(int)*10);
    foo(array);   // Not OK.
    

Now pick the solution that is least harmful.

现在选择危害最小的解决方案。

#2


2  

struct arrayContainerTen{
  int data[10];
}
void aFunction(struct arrayContainerTen *pAnArray)
{
    size_t size = sizeof(pAnArray->data);
}
main()
{
     arrayContainerTen anArray;
     aFunction(&anArray);
}

#3


1  

There's no harm in writing it like this. But just be aware that the compiler will not enforce the requirement. A declaration like that is treated by the compiler as if you'd written.

这样写是没有害处的。但请注意,编译器不会强制执行该要求。像这样的声明由编译器处理,就像你写的一样。

int foo(int *arg);

#1


2  

If you want to insist on getting an array of size 10, you can use:

如果你想坚持获得一个10号的数组,你可以使用:

int foo (int (*arg)[10]);

The ill-side effects of this are:

这种不良影响是:

  1. In the function, you have to use:

    在函数中,您必须使用:

    (*arg)[index] 
    

    instead of just

    而不仅仅是

    arg[index]
    
  2. The calling function must use:

    调用函数必须使用:

    int array[10];
    foo(&array);
    

    instead of

    int array[10];
    foo(array);
    
  3. You cannot use an array that has more than 10 elements.

    您不能使用包含10个以上元素的数组。

    int array[20];
    foo(&array);   // Not OK.
    
  4. You cannot use a malloced array.

    您不能使用malloced数组。

    int* array = malloc(sizeof(int)*10);
    foo(array);   // Not OK.
    

Now pick the solution that is least harmful.

现在选择危害最小的解决方案。

#2


2  

struct arrayContainerTen{
  int data[10];
}
void aFunction(struct arrayContainerTen *pAnArray)
{
    size_t size = sizeof(pAnArray->data);
}
main()
{
     arrayContainerTen anArray;
     aFunction(&anArray);
}

#3


1  

There's no harm in writing it like this. But just be aware that the compiler will not enforce the requirement. A declaration like that is treated by the compiler as if you'd written.

这样写是没有害处的。但请注意,编译器不会强制执行该要求。像这样的声明由编译器处理,就像你写的一样。

int foo(int *arg);