大小数组的目的是什么,因为函数参数是 c 和 c++? [英] What is the purpose of sized array as function argument is c and c++?

查看:25
本文介绍了大小数组的目的是什么,因为函数参数是 c 和 c++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下功能:

void func1(int unsized_array[]){}
void func2(int sized_array[10]){}
void func3(int *pointer){}

根据结果:

    std::cout << std::is_same<decltype(func1), decltype(func2)>::value << std::endl;
    std::cout << std::is_same<decltype(func2), decltype(func3)>::value << std::endl;
    std::cout << std::is_same<decltype(func3), decltype(func1)>::value << std::endl;

这三个函数的类型是一样的.同样在函数func2里面,sizeof操作符没有提供所有组合的数组元素的大小.

Types of these 3 functions is the same. Also inside the functionfunc2, sizeofoperator does not provide the size of array elements all combined.

那么大小数组作为函数参数(如func2)的目的是什么?

So what is the purpose of sized array as function argument (like func2)?

推荐答案

  1. 由于数组声明符通常可能有大小表达式(例如在定义一个数组而不是在参数中时,或者在声明包含数组作为子部分的参数时,例如指向数组的指针时),它将它排除在语法之外比将它无害地保留需要更多的工作.(C 中有许多结构可以导致无效,例如没有副作用的语句表达式 (3*4;)、没有副作用的逗号表达式的左操作数、a函数或带有空体的循环等.排除对语言没有影响的东西需要大量工作.)

  1. Since array declarators generally may have size expressions (as when defining an array other than in a parameter or when declaring a parameter that contains an array as a sub-part, such as a pointer to an array), it would take more work to exclude it from the grammar than to leave it in harmlessly. (There are numerous constructions in C that can result in no effect, such as a statement expression with no side effects (3*4;), the left operand of a comma expression with no side effects, a function or loop with an empty body, and so on. It would take a lot of work to exclude things that have no effect from the language.)

当存在 static 关键字时,参数中的数组大小确实会改变含义;void func2(int asd[static 10]) 声明了一个函数,该函数必须传递一个指向至少十个元素中的第一个元素的指针,这与其他声明的含义不同.(这会加剧从数组参数声明的语法中排除大小表达式的问题,因为有必要禁止裸大小而不是带有 static 的大小.)

Array sizes in parameters do alter the meaning when the static keyword is present; void func2(int asd[static 10]) declares a function that must be passed a pointer to the first of at least ten elements, which is different from the meaning of the other declarations. (And this would compound the problem of excluding a size expression from the grammar of an array parameter declaration, as it would be necessary to ban a bare size but not one with static.)

数组大小对于函数实现者和函数用户来说可能是有用的文档.

The array size may be useful documentation to the function implementor and to the function user.

评估数组大小.例如,如果定义为 void func2(int asd[printf(Hello")]) {},则在调用该函数时将打印Hello".(有些编译器可能不会这样做;C 标准对此不清楚.)

The array size is evaluated. If, for example, the definition is void func2(int asd[printf("Hello")]) {}, then "Hello" will be printed when the function is called. (Some compilers might not do this; the C standard is unclear on it.)

如果编译器发现函数使用的元素数量超过规定数量或调用者传递的元素数量少于规定数量,则编译器可以使用大小表达式发出警告.(除非使用 static,否则 Clang 11 似乎不做前者,也不做后者.)

A compiler could use the size expression to emit warnings if it sees the function uses more than the stated number of elements or a caller passes fewer than the stated number. (Clang 11 appears not to do the former and not to do the latter unless static is used.)

这篇关于大小数组的目的是什么,因为函数参数是 c 和 c++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆