Skip to main content

Arrays

Amu_Ke_Fundye

Arrays


  • Array is a collection of similar elements having same data type, accessed using a common name.
  • Array elements occupy contiguous memory locations.
  • Array indices start at zero in C, and go to one less than the size of the array.

Declaration of an Array:
type variable[num_elements];
Example: int A[100];
  • It creates an array A with 100 integer elements.
  • The size of an array A can’t be changed.
  • The number between the brackets must be a constant.

Initialization of an Array:
  • int A[5]= {1,2,3,4,5}; /*Array can be initialized during declaration*/
  • int A[5]={1,2,3}; /* Remaining elements are automatically initialized to zero*/
  • int A[5]={1,[1]=2, 3,4,[4]=0};/* Array element can be initialized by specifying its index location*/

Problems with Arrays:
  • There is no checking at run-time or compile-time to see whether reference is within array bounds.
  • Size of array must be known at compile time.

Example-1: Read the 10 values into an array of size 10.
void main()
{
int A[10], i;
for (i=0; i<10; i++)
{
printf(“Enter the number %d”, i+1);
scanf(“%d”, &a[i]);
}

Example-2: Print the 10 values of an Array A.
void main()
{
int A[10], i;
for (i=0; i<10; i++)
{
printf(“%d “, A[i]);
}
}

Pointers & Arrays: Let a[10] be an array with 10 elements.
  • The name a of the array is a constant expression, whose value is the address of the 0th location.
  • An array variable is actually just a pointer to the first element in the array.
  • You can access array elements using array notation or pointers.
  • a[0] is the same as *a
  • a[1] is the same as *(a + 1)
  • a[2] is the same as *(a + 2)
  • a = a+0 = &a[0]
  • a+1 = &a[1]
  • a+i = &a[i]
  • &(*(a+i)) = &a[i] = a+i
  • *(&a[i]) = *(a+i) = a[i]
  • Address of an element i of array a = a + i * sizeof(element)

Multi-Dimensional Array
In C language, one can have arrays of any dimensions. Let us consider a 3 × 3 matrix
3 × 3 matrix for multi-dimensional array
To access the particular element from the array, we have to use two subscripts; one for row number and other for column number. The notation is of the form a [i] [j], where i stands for row subscripts and j stands for column subscripts.
We can also define and initialize the array as follows
Note: Two Dimensional Array b[i][j]
  • For Row Major Order: Size of b[i][j] = b + ( Number of rows * i + j )*sizeof(element)
  • For Column Major Order: Size of b[i][j] = b + ( Number of Columns * j + i )*sizeof(element)
  • *(*(b + i) + j) is equivalent to b[i][j]
  • *(b + i) + j is equivalent to &b[i][j]
  • *(b[i] + j) is equivalent to b[i][j]
  • • b[i] + j is equivalent to &b[i][j]
  • (*(b+i))[j] is equivalent to b[i][j]

Strings
In C language, strings are stored in an array of character (char) type along with the null terminating character “\0″ at the end.

Printf and scanf use “%s” format character for string. Printf print characters up to terminating zero. Scanf read characters until whitespace, store result in string, and terminate with zero.

Example:
char name[ ] = { ‘G’, ‘A’, ‘T’,’E’, ‘T’, ‘O’, ‘P’, ‘\O’};
OR
char name[ ] = “GATETOP”;
  • ‘\0′ = Null character whose ASCII value is O.
  •  ‘0’ = ASCII value is 48.
  • In the above declaration ‘\0′ is not necessary. C inserts the null character automatically.
  •  %s is used in printf ( ) as a format specification for printing out a string.
  • All the following notations refer to the same element:  name [i] ,  * (name + i),  * (i + name),  i [name]

Regards 
Amrut Jagdish Gupta

Comments