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

Arrays, #question.Write a c++ program to accept and print a 1_d array

#question.Write a c++ program to accept and print a 1_d array

Source code for decoe to code, i am using mobile phone if i want to communi...

i am using mobile phone if i want to communicate via massage but that should be very secret

Padovan string, example of padovan string program

example of padovan string program

C program to check even & odd no, C Program to Check  EVEN & ODD NO   ...

C Program to Check  EVEN & ODD NO   main() {           int n, a;           clrscr();           printf("ENTER ANY NUMBER: ");           scanf("%d", &n);

Program''s, why is programming is needed now-a-days.what is the benefit of ...

why is programming is needed now-a-days.what is the benefit of colleges to apply heavy loads on students head.

Loops, how do i print out invalid input?

how do i print out invalid input?

Virtual memory implementation, I need help with virtual memory manager impl...

I need help with virtual memory manager implementation in C

Explain zero based addressing, Explain zero based addressing. - Array s...

Explain zero based addressing. - Array subscripts always start at zero. - These subscript values are used to identify elements in the array. - As subscripts start at 0, a

Programming assignment, Introduction. In this assignment you are required t...

Introduction. In this assignment you are required to simulate a maze traversal using so called recursive backtracking (the algorithm is given below). The grid of #s and 0s in the f

Explain increment and decrement operators, Increment and Decrement Operator...

Increment and Decrement Operators.   The operator for increment is '++' and decrement is '-'. These operators enhances or decrease the value of a variable on which they are ope

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