Description
This article describes how to get the size of each dimension of a
multidimensional array with the ARRAYSIZE macro.
ArraySize.cpp
ArraySize.h
Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.
This macro is defined in the "Pascal open array parameter support" file
(...\Include\Vcl\SYSOPEN.H) as follows:
#define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
This project contains a form and a TButton: TButton1.
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//Two-dimensional arrays
double Array2D[1000][110];
ShowMessage( "DIM1 =" + String( ARRAYSIZE(Array2D) )
+ "\nDIM2 =" + String( ARRAYSIZE(Array2D[0]) ) );
//-------------------------------------------------------------
//Three-dimensional arrays
float Array3D[2][3][4];
ShowMessage( "DIM1 =" + String( ARRAYSIZE(Array3D) )
+ "\nDIM2 =" + String( ARRAYSIZE(Array3D[0]) )
+ "\nDIM3 =" + String( ARRAYSIZE(Array3D[0][0]) ) );
//Set the array elements to zero:
for ( int x=0; x< ARRAYSIZE(Array3D); x++ )
for ( int y=0; y< ARRAYSIZE(Array3D[0]); y++ )
for ( int z=0; z< ARRAYSIZE(Array3D[0][0]); z++ )
Array3D[x][y][z] = 0;
String S;
//Display the array elements:
for ( int x=0; x< ARRAYSIZE(Array3D); x++ )
for ( int y=0; y< ARRAYSIZE(Array3D[0]); y++ )
for ( int z=0; z< ARRAYSIZE(Array3D[0][0]); z++ )
{
S += "Array3D["
+ String(x) + "]["
+ String(y) + "]["
+ String(z) + "]= "
+ String( Array3D[x][y][z] ) + " ";
}
ShowMessage( S );
//-------------------------------------------------------------
//Four-dimensional arrays
float Array4D[10][11][16][2];
ShowMessage( "DIM1 =" + String( ARRAYSIZE(Array4D) )
+ "\nDIM2 =" + String( ARRAYSIZE(Array4D[0]) )
+ "\nDIM3 =" + String( ARRAYSIZE(Array4D[0][0]) )
+ "\nDIM4 =" + String( ARRAYSIZE(Array4D[0][0][0]) ) );
}
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif