The basic usage of array in C + + programming _c language

Source: Internet
Author: User
Tags constructor

You can use the array subscript operator ([]) to access individual elements of an array. If you use a one-dimensional array in a subscript-free expression, the group name evaluates to a pointer to the first element in the array.

Using_arrays.cpp
int Main () {
 char charray[10];
 char *pch = Charray; Evaluates to a pointer.
 char ch = charray[0]; Evaluates to the value of the the "the".
 ch = charray[3]; Evaluates to the value of the fourth element.
}

When you use multidimensional arrays, you use various combinations in an expression.

Using_arrays_2.cpp
//compile with:/ehsc/w1
#include <iostream>
using namespace std;
int main () {
 double multi[4][4][3];//Declare the array.
 Double (*p2multi) [3];
 Double (*p1multi);

 cout << multi[3][2][2] << "\ n"; C4700 use three subscripts.
 P2multi = multi[3];    Make P2multi point to
          //fourth "plane" of multi.
 P1multi = multi[3][2];   Make P1multi point
          to//fourth plane, third row
          //of multi.
}

In the preceding code, multi is a three-dimensional array of type double. The P2multi pointer points to an array of type double size three. In this example, the array is used for one, two, and three subscript. Although it is more common to specify all subscripts, as shown in the cout statement, the following statement is cout, sometimes useful when selecting a specific subset of an array element.

Initializing an array
if the class has a constructor, an array of that class is initialized by the constructor. If the entries in the initializer list are less than the elements in the array, the default constructor is used for the remaining elements. If you do not define a default constructor for a class, the initializer list must be complete, that is, each element in the array must have an initializer.
Consider the point class that defines two constructors:

Initializing_arrays1.cpp
class Point
{public
: Point
 ()//Default constructor.
 {
 } point
 (int, int)//construct from two ints
 {
 }
};

An array of point objects can is declared as follows: Point
apoint[3] = {Point
 (3, 3)  //use int, int constructor.
};

int main ()
{
}

The first element of Apoint is constructed using the constructor point (int, int), and the remaining two elements are constructed using the default constructor.
An array of static members (whether const) can be initialized in its definition (external to the class declaration). For example:

Initializing_arrays2.cpp
class windowcolors
{public
:
 static const char *RGSZWINDOWPARTLIST[7];

const char *windowcolors::rgszwindowpartlist[7] = {
 "Active title bar", "Inactive title bar", "title bar Text", 
    "menu Bar", "menu bar Text", "Window Background", "Frame"};
int main ()
{
}

An array in an expression
when an identifier for an array type appears in an expression other than sizeof, address-of (&), or a referenced initialization, the identifier is converted to a pointer to the first array element. For example:

Char szerror1[] = "Error:disk Drive not ready.";
char *psz = SzError1;

The pointer psz the first element that points to the szError1 of the array. Note that unlike pointers, arrays are not modifiable left values. Therefore, the following assignment is illegal:

SzError1 = Psz;

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.