Submit Your Site To The Web's Top 50 Search Engines for Free!       ExactSeek: Relevant Web Search

Visitors

Flag Counter

Total Pageviews

Tuesday, February 19, 2013

Array in C, C++ and C#

1. What is an Array?

An array is a collection of same type of elements which are sheltered under a common name. An array can be visualised as a row in a table, whose each successive block can be thought of as memory bytes containing one element. Look at the figure below : An Array of four elements, start from Index-0 to Index-3.
2. How to Define an Array?

1. C and C++
When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros. In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }. For example:

    int odd[5] = { 1, 3, 5, 7, 11 };
When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty [ ]. In this case, the compiler will assume a size for the array that matches the number of values included between braces { }:

     char odd[] = { 'B', 'M', 'W'};  

An array is defined as following :
<type-of-array> <name-of-array> [<number of elements in array>];
  • type-of-array: It is the type of elements that an array stores. If array stores character elements then type of array is ‘char’. If array stores integer elements then type of array is ‘int’. Besides these native types, if type of elements in array is structure objects then type of array becomes the structure.
  • name-of-array: This is the name that is given to array. It can be any string but it is usually suggested that some can of standard should be followed while naming arrays. At least the name should be in context with what is being stored in the array.
  • [number of elements]: This value in subscripts [] indicates the number of elements the array stores.

2. C# (C Sharp)

C# arrays are zero indexed; that is, the array indexes start at zero. Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences that you should be aware of. When declaring an array, the square brackets ([]) must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#.
     int[] table; // not int table[];
Another detail is that the size of the array is not part of its type as it is in the C language. This allows you to declare an array and assign any array of int objects to it, regardless of the array's length.
     int[] numbers; // declare numbers as an int array of any size
     numbers = new int[10];  // numbers is a 10-element array
     numbers = new int[20];  // now it's a 20-element array
Array-of-arrays (jagged) or Array 2- Dimensional:
     byte[][] scores;
Multidimensional arrays:
     string[,] names;
     string[,] names = new string[5,4];
You can also have larger arrays. For example, you can have a three-dimensional rectangular array:
     int[,,] buttons = new int[4,5,3];
You can even mix rectangular and jagged arrays. For example, the following code declares a single-dimensional array of three-dimensional arrays of two-dimensional arrays of type int:
     int[][,,][,] numbers;
C# provides simple and straightforward ways to initialize arrays at declaration time by enclosing the initial values in curly braces ({}). The following examples show different ways to initialize different kinds of arrays. If you do not initialize an array at the time of declaration, the array members are automatically initialized to the default initial value for the array type. Also, if you declare the array as a field of a type, it will be set to the default value null when you instantiate the type.
     int[] numbers = new int[5] {1, 2, 3, 4, 5};
     string[] names = new string[] {"Matt", "Joanne", "Robert"}; 
You can also omit the new operator if an initializer is provided, like this:
    int[] numbers = {1, 2, 3, 4, 5};
    string[] names = {"Matt", "Joanne", "Robert"};

0 comments

Post a Comment

loading