Explain the ways of initializing the arrays, C/C++ Programming

Assignment Help:

Various ways of initializing the Arrays.

- The for loop initializes 10 elements with the value of their index.

 

   void main()

    {

                const int size = 10; 

                int arr[size];

 

for(int i = 0; i < size ; i++ ) // You can declare a

                variable here in C++.

                  {

                       arr[i] = i;

                   }

      }                      

 

  • An array can be explicitly initialized as follows.

e.g.

                int arr[3] = {0,1,2};

- An explicitly initialized array need not specify size but if specified the number of elements provided must not exceed the size. If the size is given and some elements are not explicitly initialized they are set to zero

                e.g.

 

                int arr[] = {0,1,2};

 

     int arr1[5] = {0,1,2}; // Initialized as {0,1,2,0,0}

      const char a_arr1[] = {'c'.'+','+'}          //size = 3;

 

                     const char a_arr2[] = {"c++"}  //size = 4 because of 

                                            null character at the 

                                                                 end of  the string;

  const char a_arr3[6] = "Daniel"; // ERROR; Daniel has 7  elements

 


Related Discussions:- Explain the ways of initializing the arrays

Compter graphices, program that generate university statistical bar graph u...

program that generate university statistical bar graph using 3d function

Destructor , How can I handle a destructor that fails? Need help please pro...

How can I handle a destructor that fails? Need help please provide example also.

Polishing game, Byteland county is very famous for luminous jewels. Luminou...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Webcam driver static library or dll required for windows, Webcam Driver sta...

Webcam Driver static library or DLL required for Windows I want a .dll or .lib file using which i will take a snapshot from the webcam and it can't turn on the webcam LED while

Luminous Jewels - The Polishing Game, within 2 mins give me answer pllzzzzz...

within 2 mins give me answer pllzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

In binary mode how can i open a stream?, A: Use std::ios::binary. Some o...

A: Use std::ios::binary. Some operating systems differentiate among text and binary modes. In text mode, end-of-line sequences and perhaps other things are translated; in binary

When i throw this object, A: Depends. Might be "zero" Objects which are ...

A: Depends. Might be "zero" Objects which are thrown must have a publicly accessible copy-constructor. The compiler is permitted to generate code which copies the thrown object

Padovan.., count the number of string in n-th padovan string

count the number of string in n-th padovan string

AREAS, Write a program to find the area under the curve y = f(x) between x ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Area under the curve, Write a program to find the area under the curve y ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd