top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Arrays in C/C++: An introduction

+1 vote
233 views

An array is a collection of same type of elements which are sheltered under a common name.

Instead of declaring individual variables, such as elem0, elem1, ...one can declare one array variable such as elem and use elem[0], elem[1], ...to represent individual variables. A specific element in an array is accessed by an index.

Example

+===================================================+
| elem[0]    |  elem[1]    | elem[2]    | elem[3]   |
+===================================================+

Defining an Array

type arrayName [ arraySize ]; //single-dimensional array declaration 

<type> can be any valid C data type.
<arraySize> must be an integer constant greater than zero

Initializing an Array

Example: Integer Array - Individual Initialization

int a[5];
int i = 0;
for(i=0;i<sizeof(a);i++) 
{ 
  a[i] = i; 
} 

Example: Integer Array - Initializing array at the time of declaration.

int a[] = {1,2,3,4,5};

Example: String Initialization

char s[] = "Hello";
or 
char s[] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Identical to the previous one 

Example: Array of Pointers

int main()
{
    char *ptr1 = "Pradeep";
    char *ptr2 = "Kohli";
    char *ptr3 = "India"; 

    //Declaring an array of 3 char pointers
    char* a[3]; 

    // Initializing the array with values
    a[0] = ptr1;
    a[1] = ptr2;
    a[2] = ptr3; 
}

Example: Pointer to an Array

type (*arrayName)[arraySize]

For example :

int(*p)[5];

Difference between a pointer and an array

All the above example can give a mislead about an array and can feel also like a pointer. But here are few points which differentiates the pointer with an array -

  1. A pointer is a place in memory that keeps address of another place inside where as an array is a single, pre allocated chunk of contiguous elements (all of the same type), fixed in size and location.
  2. Pointer can’t be initialized at definition (it first need to be allocated). Array can be initialized at definition.

    int num[] = { 1, 2, 3};

  3. Pointer is dynamic in nature. The memory allocation can be resized or freed later. where as arrays are static in nature. Once memory is allocated , it cannot be resized or freed dynamically.
  4. Array name is constant while pointer name is variable.
posted Jun 8, 2014 by Pardeep Kohli

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

An array which can have arrays as its element are called multi-dimensional array. Or in simple words if array has more then one subscript then it is called multi-dimensional array.

Declaration
type arrayName [SubscriptSize1 ][SuscriptSize2]...[SubscriptSizeN];

Examples

char arr[5][10];
char arr[50][5][10];

In example 1 arr means 5 rows and 10 columns. Since its a char array so each row can contain a string of max 10 characters and total 5 such strings are possible.
In example 2 arr means 50 arrays of example 1.

Multi-dimensional Array Visualization

Multi-Dimensional Array

Once we visulize the array in the following manner nothing is complex see the following figure which is a representation of array[3][4][5].

Memory Representation

Though in the above example we have seen the array in the matrix form or matrix of matrix form but in reality they are stored in the contiguous fashion in the memory.

a[0][3] = a[0][0] + Size of Data Type

Example:
Assume that we have an array a as a[3][3] and the base address i.e. a[0][0] is 1000, and size of data type is say 2. Then it will have following address in the memory -

a[0][0] = 1000
a[0][4] = 1002
a[0][2] = 1004
a[1][0] = 1006
a[1][5] = 1008
a[1][2] = 1010
a[2][0] = 1012
a[2][6] = 1014
a[2][2] = 1016

Note: Remember that all the leaf element/variable of an multi-dimentional array should/would be of same type.

READ MORE
...