<tfoot id='r8YUI'></tfoot>
  • <legend id='r8YUI'><style id='r8YUI'><dir id='r8YUI'><q id='r8YUI'></q></dir></style></legend>

          <bdo id='r8YUI'></bdo><ul id='r8YUI'></ul>
        <i id='r8YUI'><tr id='r8YUI'><dt id='r8YUI'><q id='r8YUI'><span id='r8YUI'><b id='r8YUI'><form id='r8YUI'><ins id='r8YUI'></ins><ul id='r8YUI'></ul><sub id='r8YUI'></sub></form><legend id='r8YUI'></legend><bdo id='r8YUI'><pre id='r8YUI'><center id='r8YUI'></center></pre></bdo></b><th id='r8YUI'></th></span></q></dt></tr></i><div id='r8YUI'><tfoot id='r8YUI'></tfoot><dl id='r8YUI'><fieldset id='r8YUI'></fieldset></dl></div>

        <small id='r8YUI'></small><noframes id='r8YUI'>

      1. 我们什么时候需要将数组的大小作为参数传递

        when do we need to pass the size of array as a parameter(我们什么时候需要将数组的大小作为参数传递)

            <tbody id='Y4rL7'></tbody>

                <legend id='Y4rL7'><style id='Y4rL7'><dir id='Y4rL7'><q id='Y4rL7'></q></dir></style></legend>

                <small id='Y4rL7'></small><noframes id='Y4rL7'>

                <tfoot id='Y4rL7'></tfoot>
                  <bdo id='Y4rL7'></bdo><ul id='Y4rL7'></ul>
                  <i id='Y4rL7'><tr id='Y4rL7'><dt id='Y4rL7'><q id='Y4rL7'><span id='Y4rL7'><b id='Y4rL7'><form id='Y4rL7'><ins id='Y4rL7'></ins><ul id='Y4rL7'></ul><sub id='Y4rL7'></sub></form><legend id='Y4rL7'></legend><bdo id='Y4rL7'><pre id='Y4rL7'><center id='Y4rL7'></center></pre></bdo></b><th id='Y4rL7'></th></span></q></dt></tr></i><div id='Y4rL7'><tfoot id='Y4rL7'></tfoot><dl id='Y4rL7'><fieldset id='Y4rL7'></fieldset></dl></div>

                  本文介绍了我们什么时候需要将数组的大小作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对在 C/C++ 中传递数组有点困惑.我看到了一些签名是这样的情况

                  I am a little bit confused about pass an array in C/C++. I saw some cases in which the signature is like this

                  void f(int arr[])
                  

                  有些是这样的

                  void f(int arr[], int size)
                  

                  谁能详细说明有什么区别以及何时以及如何使用它?

                  Could anybody elaborate what's the difference and when and how to use it?

                  推荐答案

                  首先,传递给函数的数组实际上传递的是指向数组第一个元素的指针,例如,如果你有

                  First, an array passed to a function actually passes a pointer to the first element of the array, e.g., if you have

                  int a[] = { 1, 2, 3 };
                  f(a);
                  

                  然后,f()&a[0] 传递给它.因此,在编写函数原型时,以下内容是等效的:

                  Then, f() gets &a[0] passed to it. So, when writing your function prototypes, the following are equivalent:

                  void f(int arr[]);
                  void f(int *arr);
                  

                  这意味着数组的大小丢失了,而f(),一般情况下是无法确定大小的.(这就是我更喜欢 void f(int *arr) 形式而不是 void f(int arr[]) 的原因.)

                  This means that the size of the array is lost, and f(), in general, can't determine the size. (This is the reason I prefer void f(int *arr) form over void f(int arr[]).)

                  f() 有两种情况不需要信息,在这两种情况下,不需要额外的参数就可以了.

                  There are two cases where f() doesn't need the information, and in those two cases, it is OK to not have an extra parameter to it.

                  首先,arr 中有一些特殊的、商定的值,调用者和 f() 都认为这意味着结束".例如,人们可以同意值 0 表示完成".

                  First, there is some special, agreed value in arr that both the caller and f() take to mean "the end". For example, one can agree that a value 0 means "Done".

                  然后可以写:

                  int a[] = { 1, 2, 3, 0 }; /* make sure there is a 0 at the end */
                  int result = f(a);
                  

                  并定义 f() 类似:

                  int f(int *a)
                  {
                      size_t i;
                      int result = 0;
                      for (i=0; a[i]; ++i)  /* loop until we see a 0 */
                          result += a[i];
                      return result;
                  }
                  

                  显然,上述方案只有在调用方和被调用方都同意并遵守约定时才有效.一个例子是 C 库中的 strlen() 函数.它通过查找 0 来计算字符串的长度.如果您传递的内容最后没有 0,则所有赌注都将关闭,您将处于未定义的行为领域.

                  Obviously, the above scheme works only if both the caller and the callee agree to a convention, and follow it. An example is strlen() function in the C library. It calculates the length of a string by finding a 0. If you pass it something that doesn't have a 0 at the end, all bets are off, and you are in the undefined behavior territory.

                  第二种情况是当您实际上没有数组时.在这种情况下, f() 接受一个指向对象的指针(在您的示例中为 int ).所以:

                  The second case is when you don't really have an array. In this case, f() takes a pointer to an object (int in your example). So:

                  int change_me = 10;
                  f(&change_me);
                  printf("%d
                  ", change_me);
                  

                  void f(int *a)
                  {
                      *a = 42;
                  }
                  

                  很好:f() 无论如何都不会对数组进行操作.

                  is fine: f() is not operating on an array anyway.

                  这篇关于我们什么时候需要将数组的大小作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                  1. <i id='X5HC2'><tr id='X5HC2'><dt id='X5HC2'><q id='X5HC2'><span id='X5HC2'><b id='X5HC2'><form id='X5HC2'><ins id='X5HC2'></ins><ul id='X5HC2'></ul><sub id='X5HC2'></sub></form><legend id='X5HC2'></legend><bdo id='X5HC2'><pre id='X5HC2'><center id='X5HC2'></center></pre></bdo></b><th id='X5HC2'></th></span></q></dt></tr></i><div id='X5HC2'><tfoot id='X5HC2'></tfoot><dl id='X5HC2'><fieldset id='X5HC2'></fieldset></dl></div>
                      <tbody id='X5HC2'></tbody>

                    <small id='X5HC2'></small><noframes id='X5HC2'>

                        <tfoot id='X5HC2'></tfoot>

                        • <bdo id='X5HC2'></bdo><ul id='X5HC2'></ul>

                            <legend id='X5HC2'><style id='X5HC2'><dir id='X5HC2'><q id='X5HC2'></q></dir></style></legend>