Array definition and initialization, array definition Initialization

Source: Internet
Author: User
Tags array definition

Array definition and initialization, array definition Initialization

I. Definition

The dimension of the array must be defined by a constant expression greater than or equal to 1.

Integer const object initialized by a nominal value constant, enumerative constant, or constant expression;

Ii. Initialization

1. Display initialization array elements

* The built-in array defined in the function body, whose elements are initialized to 0;

* Built-in arrays defined in the function body, whose elements are not initialized;

* No matter where the array is defined, if its element is of the class type, the default constructor of the class is automatically called for initialization. If the class does not have the default constructor, the display initialization must be provided for the elements of the array.

2. Special Character Array

3. Directly copying and assigning values to arrays is not allowed.

// SHUZU. cpp: Defines the entry point for the console application. // # include "stdafx. h "# include <iostream >#include <string> using std: string; int get_size () {static int I = 0; I = I + 1; return I ;} class testClass {public: protected: private: int a; char * p ;}; int atest [10]; testClass atc [10]; int _ tmain (int argc, _ TCHAR * argv []) {const unsigned buf_size = 512, max_files = 20; int staff_size = 27; const unsigned sz = get_size (); // sz is a const object, however, the value of get_size needs to be run multiple times to know that char input_buffer [buf_size]; // buf_size is a const constant string fileTable [max_files + 1]; // OK max_files is a const constant, max_files + 1 can be calculated as 21 // double salaries [staff_size] during compilation; // error non const variable/int test_scores [get_size ()]; // error not const expression // int vals [sz]; // error size not known until run time // display the initialization array element int btest [10]; testClass btc [10]; // a special character array char ca1 [] = {'C', '+', '+ '}; // 3-dimensional char [] = {'C', '+', '+', '\ 0 '}; // 4-dimensional char A3 [] = "c ++"; // 4-dimensional null terminator added automatically // The array cannot be directly copied or assigned a value to int ia [] = {0, 1, 2}; // int ia2 [] (ia); // error int ia3 []; // ia3 = ia; // error return 0 ;}


C # define array Initialization

String [] mycheck;
Mycheck = new string [36];
For (int I = 0; I <36; I ++)
{
Mycheck [I] = "1 ";
}

Multi-dimensional array definition and initialization in C Language

A two-dimensional array is defined as follows:
Array name of type identifier [constant expression 1] [constant expression 2 ];
For example:
Int a [2] [3];
Float B [3] [10];
Two-dimensional array initialization methods are available:
(1) Branch initialization, such:
Static int a [2] [3] = {1, 2, 3 ,}, {4, 5, 6 }};
(2) Unified initialization, such:
Static int a [2] [3] = {1, 2, 3, 4, 5, 6 };

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.