C Arrays: How to Declare, Initialize and Access Elements ?

C Arrays

C arrays is a collection of data items, with all of the same type and accessed using a common name. A one-dimensional(1-D) array is like a list;  A two dimensional(2-D) array is like a table.

In C language there is no limits on the number of dimensions in an array, though specific implementations may.

C Arrays | Tech-Mirror
C Arrays | Tech-Mirror

Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

Always, Contiguous (adjacent) memory locations are used to store array elements in memory.


How to Declare Arrays in C?

  • An Array variables are declared identically to variables of their data type like int, float etc., except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array.
  • Dimensions used when declaring c arrays must be constant expressions or positive integral constants.
  • Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets.

Declaring a C arrays:


Initializing Arrays

  • An arrays may be initialized when they are declared, just as any other variables.
  • Put the initialization data in curly {} braces following with the equals sign.
  • An array may be partially initialized, by providing fewer data items than the size of the array.  The remaining array elements will be automatically initialized to 0(zero).
  • If an array is to be completely initialized, the dimension of the array is not required.  The compiler will automatically allocate the size the array to fit the initialized data.

Initialization of an Array:


Accessing an array elements:

Output:

value of a1[0] is 10
value of a1[1] is 20
value of a1[2] is 30
value of a1[3] is 40
value of a1[4] is 50

Leave a Reply

Your email address will not be published. Required fields are marked *