将大小数组作为函数参数的目的是c和c++?

What is the purpose of sized array as function argument is c and c++?(将大小数组作为函数参数的目的是c和c++?)
本文介绍了将大小数组作为函数参数的目的是c和c++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Consider the following functions:

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

According to the result of:

    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;

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

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

解决方案

  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.)

  2. 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.)

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

  4. 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.)

  5. 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++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)