Important points to remember about arrays

  • An array is a sequence of contiguous memory cells, all of the same size.
  • The array name is an alias for a const-typed pointer to the first cell of the array.
  • Array doesn't initializes it's values,during definition
int a[10];
for(int i=0;i<10;i++)
printf("%d\n",a[i]);
//The output of this snippet will be some random garbage value
//hence we can say that arrays are not initialized during definition
A smart way to initialize arrays is
int a[10]={0};
for(int i=0;i<10;i++)
printf("%d\n",a[i]);
// output of this snippet will be 10 zeroes which are in a new line each
  • Array subscripts are valid only when used to access members of an array and only within the declared limits of the array.
int a[10]={0};
printf("%d\n",a[100]);
printf("%d\n",a[-1]);
//both the statements raises error because a[100] is not in range
//and a[-1] is not a valid index in C unlike python
  • The first element in the array is base address of array and ever element can be accessed using base address and index
int a[10]={1,2,3,4,5,6,7,8,9,0};
printf("%d\n",*(a));
printf("%d\n",*(a+1));
printf("%d\n",*(a+2));
/// these statements prints 0th index element ,1st index element and
//2nd index element each on a new line
  • If only a few values in a array are initialized the remaining all other values are initialized to a 'ZERO'
int a[10]={1,2,3};
for(int i=0;i<10;i++)
printf("%d\n",a[i]);
//this snippet prints 1,2,3 each on a new line and next seven zeros on next lines

  • Arrays are passed to and returned from functions as pointers. This functionality reduces the overhead of passing more number of variables .
  • Arrays are static and doesn't change their size dynamically unlike lists in python.
  • In order to dynamically change the size of array we have different data structures to perform special tasks as insertion at the middle of array , delete an element at the middle of array and
attach new elements at end of array e.t.c
  • Usually array can't do these above mentioned functions in O(1) but data structures does that job.
  • In order to all these tasks using an array in c we need to do a tedious work..
Hope this helps
Thank you for reading.....