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

          <bdo id='HhTeQ'></bdo><ul id='HhTeQ'></ul>

      1. <small id='HhTeQ'></small><noframes id='HhTeQ'>

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

        C++ 将数组的引用传递给函数

        C++ Pass a reference to an array to function(C++ 将数组的引用传递给函数)

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

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

                • <bdo id='Thkxb'></bdo><ul id='Thkxb'></ul>
                  本文介绍了C++ 将数组的引用传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  给函数 void main() 和 void hello(byte* a[4]).Main 函数有一个四个字节的数组.数组的引用需要传递给函数 hello 进行操作.我希望正确的语法是:

                  Given to functions void main() and void hello(byte* a[4]). Main function has an array of four bytes. The array's reference needs to be passed to the function hello for manipulation. I would expect the right syntax to be:

                  void hello(byte* a[4]){
                   // Manipulate array
                   a[0] = a[0]+1;
                  }
                  
                  void main(){
                   byte stuff[4] = {0,0,0,0};
                   hello(&stuff);
                   // hopefully stuff is now equal {1,0,0,0}
                  }
                  

                  或者我看到其他人使用这种形式的 decaration:

                  Alternatively I see others using this form of decaration:

                  void hello(byte (&a)[4])
                  

                  这是正确的做法吗?

                  推荐答案

                  这里有许多不同的选项,具体取决于您想在这里做什么.

                  There are many different options here depending on what you want to do here.

                  如果你有一个 byte 对象的原始数组,你可以把它传递给一个像这样的函数:

                  If you have a raw array of byte objects, you can pass it into a function like this:

                  void hello(byte arr[]) {
                     // Do something with arr
                  }
                  
                  int main() {
                     byte arr[4];
                     hello(arr);
                  }
                  

                  将数组传递给函数的机制(将指向数组第一个元素的指针传递给函数)的功能类似于按引用传递:您对 arrhello 中的 code> 将保留在 main 中,即使您没有明确传递对它的引用.但是,hello 函数不会检查数组的大小是否为 4 - 它会将 任意 个字节的数组作为输入.

                  The mechanism by which the array is passed into the function (a pointer to the first element of the array is passed to the function) functions similarly to pass-by-reference: any changes you make to arr in hello will stick in main even though you didn't explicitly pass in a reference to it. However, the hello function won't check whether the array has size four or not - it'll take in as input an array of any number of bytes.

                  你也可以写

                  void hello(byte (&arr)[4]) {
                     // ...
                  }
                  
                  int main() {
                      byte arr[4];
                      hello(arr);
                  }
                  

                  语法 byte (&arr)[4] 表示对四个字节数组的引用".这将通过引用显式地将数组传递给 hello,并且它将检查数组的大小以确保它是正确的.然而,这是非常不寻常的语法,在实践中很少见.

                  The syntax byte (&arr)[4] means "a reference to an array of four bytes." This explicitly passes the array by reference into hello, and it will check the size of the array to make sure it's correct. However, this is very unusual syntax and rarely seen in practice.

                  但也许最好的主意是不使用原始数组并使用 std::array 代替:

                  But perhaps the best idea is to not use raw arrays and to use std::array instead:

                  void hello(std::array<byte, 4>& arr) {
                      // Do something with arr
                  }
                  
                  int main() {
                      std::array<byte, 4> arr;
                      hello(arr);
                  }
                  

                  现在,字节数组的语法中没有奇怪的括号,也不必担心大小检查.一切都得到了正确处理,因为 std::array 是一种对象类型,它具有常规对象类型的所有优点.我建议采用最后一种方法,而不是其他方法.

                  Now, there's no weirdnesses about strange parentheses in the syntax for arrays of bytes and there's no worries about size checking. Everything is handled properly because std::array is an object type that has all the advantages of regular object types. I'd recommend going with this last approach above all the other ones.

                  这篇关于C++ 将数组的引用传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                • <legend id='alsqv'><style id='alsqv'><dir id='alsqv'><q id='alsqv'></q></dir></style></legend>
                    <bdo id='alsqv'></bdo><ul id='alsqv'></ul>
                  • <small id='alsqv'></small><noframes id='alsqv'>

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

                            <tbody id='alsqv'></tbody>