C Array

  • In C programming language provides a data structure called as Array.
  • An array is a collection of similar elements.
  • Array can store a number of elements of homogeneous type store in a sequential manner.
  • An array elements could be all ints, or all floats, or all chars, etc.
  • Array of chars is usually called as string.
  • All array elements must be are of the same data type.
  • For example: if a user wants to store marks of 100 students. One way to do this is, create 100 variables for each student. So it will be difficult to manage all variables. Therefore, this type of problem can be handled by ‘Array’.

Definition of array:

Array is a collection of the homogeneous type of data stored at the continuous memory location.
Array can be an integer array or character array. A character array is also known as a string.

array
Above diagram is a basic example of an array. The first index of an array is always the first element of an array as shown in the figure. This figure represents the array:

Array Elements:

  • The number of elements in an array defines the size of an array.
  • Size represents the maximum number of elements.
  • Array index always starts from 0.
  • Till the array element value are not given, they are supposed to contain garbage values.
  • Each element can be accessed by the user whenever it needed.
  • Array elements can be passed to a function by calling the function by value, or by reference.
  • Array elements are accessed by using array index.

Types of array:

In C, an array can be:

Points to remember:

  • You can calculate size of an array by using “sizeof”
  • If you declare an array as constant, then you can not change the values of elements.
  • Array size can be defined by #define directive

    Preprocessor replace macro size with 5 during preprocessing and then compiler compiles the program with replaced values.
  • Array does not perform bounds checking during run-time execution.

    Output:
Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.