Definition of C + + three-dimensional static array and its transfer as function

Source: Internet
Author: User

In C + +, we can define three-dimensional arrays, and we can pass them directly as parameters.

Defined:

#include <iostream>#include<windows.h>using namespacestd;Const intx =Ten;Const inty =Ten;Const intz =Ten;intMain () {DoubleFoo[x][y][z];  for(inti =0; i < x; i++) {         for(intj =0; J < y; J + +) {             for(intK =0; K < Z; k++) {Foo[i][j][k]=1.0; } }} cout<< foo[0][0][0] << Endl;//1.0System"Pause"); return 0;}

As shown above, we set a static array, so we must determine its size before defining the three-dimensional array, and for the maintainability of the program, we recommend that you define it with a const int.

To pass a three-dimensional array as a parameter:

#include <iostream>#include<windows.h>using namespacestd;Const intx =Ten;Const inty =Ten;Const intz =Ten;intBarDoublearr[][y][z]);intMain () {DoubleFoo[x][y][z];  for(inti =0; i < x; i++) {         for(intj =0; J < y; J + +) {             for(intK =0; K < Z; k++) {Foo[i][j][k]=1.0; } }} cout<< foo[0][0][0] << Endl;//1.0Bar (foo); System ("Pause"); return 0;}intBarDoubleArr[][y][z]) {cout<<"function invoked value:"<< arr[1][1][1] <<Endl; return 0;}

As shown above, the end result is:

1 function invoked value1

Note that when you pass a three-dimensional array as a parameter, the first [] of the array is empty, and the second third cannot be empty.

This makes it easy to handle three-dimensional arrays in most cases.

Definition of C + + three-dimensional static array and its transfer as function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.