Data Structures

Arrays in C

An array is a collection of items stored in continuous memory locations. The idea is to declare multiple items of the same type together.

Array declaration: In C, we can declare an array by specifying its and size or by initializing it or by both.

Array declaration by specifying size int arr[10];

Array declaration by initializing elements int arr[] = {10, 20, 30, 40}

Compiler creates an array of size 4. above is same as "int arr[4] = {10, 20, 30, 40}"

Array declaration by specifying size and initializing elements int arr[6] = {10, 20, 30, 40}

Compiler creates an array of size 6, initializes first 4 elements as specified by user and rest two elements as 0. above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"

Properties

1) In C, it is possible to have array of all types except void and functions. See this for details.

2) In C, array and pointer are different. They seem similar because array name gives the address of first element and array elements are accessed using pointer arithmetic. See array vs pointer in C for details.

3) Arrays are always passed as pointer to functions. See this for details.

4) A character array initialized with double quoted string has last element as ‘\0?. See this for details.

5) Like other variables, arrays can be allocated memory in any of the three segments, data, heap, and stack (See this for details). Dynamically allocated arrays are allocated memory on the heap, static or global arrays are allocated memory on data segment and local arrays are allocated memory on stack segment.

Accessing Array Elements: Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1. Following are few examples.

int main()
{
  int arr[5];
  arr[0] = 5;
  arr[2] = -10;
  arr[3/2] = 2; // this is same as arr[1] = 2
  arr[3] = arr[0];

  printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);

  return 0;
}

Output:

5 2 -10 5
No Index Out of bound Checking: There is no index out of bound checking in C, for example, the following program compiles fine but may produce an unexpected output when run.

// This C program compiles fine as index out of bound
// is not checked in C.
int main()
{
  int arr[2];

  printf("%d ", arr[3]);
  printf("%d ", arr[-2]);

  return 0;
}

Also, In C, it is not compiler error to initialize an array with more elements than specified size. For example, the below program compiles fine.

int main()
{

  // Array declaration by initializing it with more
  // elements than specified size.
  int arr[2] = {10, 20, 30, 40, 50};

  return 0;
}

The program won’t compile in C++. If we save the above program as a .cpp, the program generates compiler error “error: too many initializers for ‘int [2]'”

An Example to show that array elements are stored at contiguous locations

// C program to demonstrate that array elements are stored
// contiguous locations
int main()
{
  // an array of 10 integers. If arr[0] is stored at
  // address x, then arr[1] is stored at x + sizeof(int)
  // arr[2] is stored at x + sizeof(int) + sizeof(int)
  // and so on.
  int arr[5], i;

  printf("Size of integer in this compiler is %u\n", sizeof(int));

  for (i=0; i<5; i++)
    // The use of '&' before a variable name, yields
    // address of variable.
    printf("Address arr[%d] is %u\n", i, &arr[i]);

  return 0;
}

Output:

Size of integer in this compiler is 4
Address arr[0] is 2686728
Address arr[1] is 2686732
Address arr[2] is 2686736
Address arr[3] is 2686740
Address arr[4] is 2686744

Using sizeof operators with array parameters

Consider the below program.

#include<stdio.h>

void fun(int arr[]) 
{
  int i; 

  /* sizeof should not be used here to get number 
  of elements in array*/
  int arr_size = sizeof(arr)/sizeof(arr[0]); /* incorrect use of sizeof*/
  for (i = 0; i < arr_size; i++) 
    arr[i] = i; /*executed only once */
}

int main()
{
  int i; 
  int arr[4] = {0, 0 ,0, 0};
  fun(arr);

  /* use of sizeof is fine here*/
  for(i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
    printf(" %d " ,arr[i]);

  return 0;
}

Output: 0 0 0 0 on a IA-32 machine.

The function fun() receives an array parameter arr[] and tries to find out number of elements in arr[] using sizeof operator. In C, array parameters are treated as pointers (See http://geeksforgeeks.org/?p=4088 for details). So the expression sizeof(arr)/sizeof(arr[0]) becomes sizeof(int *)/sizeof(int) which results in 1 for IA 32 bit machine (size of int and int * is 4) and the for loop inside fun() is executed only once irrespective of the size of the array.

Therefore, sizeof should not be used to get the number of elements in such cases. A separate parameter for array size (or length) should be passed to fun(). So the corrected program is:

#include<stdio.h>

void fun(int arr[], size_t arr_size) 
{
  int i; 
  for (i = 0; i < arr_size; i++) 
     arr[i] = i; 
}

int main()
{
  int i; 
  int arr[4] = {0, 0 ,0, 0};
  fun(arr, 4);

  for(i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
     printf(" %d ", arr[i]);

  return 0;
}

Recent Comments

Feedback

For any feedback,email us at feedback@geeksforgeeks.org

Report a Bug

For any report or bug,email us at support@geeksforgeeks.org