Arrays in c

What is an Array ?

Arrays in c programming language is set of elements of same data type. Array is stored in continuous storage space.

If you dont know about arrays then check out datatypes in c article to know basics of an array.

C Arrays starts with index 0.

Arrays provide random access to data elements.


Declare Array in c

Array can be of any datatype. There are two types of array:

  • Static array.
  • Dynamic array.

In static array allocation of memory for elements is done statically
In dynamic array allocation of memory for elements is done dynamically.

How to declare a static array?

You must know the array size.
check out example below:

datatype array_name [ size ] ;

If you want to initialize in the same line then

datatype array_name [ size ] = { elements };

How to declare a dynamic array?

You have to know the size of array to declare a dynamic array.

If you dont know dynamic memory allocation in c then read more about dynamic memory allocation in c.

We can make use of malloc() , calloc() , realloc() , and free() to manage dynamic memory allocation.

To declare an array dynamically using malloc

//int array using malloc
int *array=(int*)malloc(n*sizeof(int));
//array accessing
*array[0]=0;

Look at the simple programming example for c arrays below:

Program to Demonstrate c Array

#include<stdio.h>
int main(int argc, char const *argv[])
{
	//here we declare an array
	int array[10];
	//its simple 
	printf("We have successfully declared an array");
	return 0;
}
c arrays

Initialize c Arrays

You can initialize array while declaring the array or you can seperately access each index of array to assign a value.

Program for initializing array

#include<stdio.h>
int main(int argc, char const *argv[])
{

	//here we declare an array
	int array[10];
	printf("\nEnter 10 elements\n");
	//here iam inputting each element using scanf
	for( int i=0 ; i<9 ; i++ )
	{
		scanf("%d",&array[i]);
	}
	printf("Array elements are:\n");
	for( int i=0 ; i<9 ; i++ )
	{
		printf("%d, ",array[i]);
	}
	return 0;
}
Initialize c arrays

Access Elements of an Array

Access an element of array by using [ ] operator by specifying the index of the element.

Program to acces Array in c

#include<stdio.h>
int main(int argc, char const *argv[])
{
	//here we declare an array
	int array[10];
	printf("\nEnter 10 elements\n");
	//here iam inputting each element using scanf
	for( int i=0 ; i<9 ; i++ )
	{
		scanf("%d",&array[i]);
	}
	printf("Array elements are:\n");
	for( int i=0 ; i<9 ; i++ )
	{//lets print each number using [ ] operator
		printf("%d, ",array[i]);
	}
	return 0;
}
Access elements of an array

Pointer to c arrays

You can create a pointer to first element of the array then you can simply access other elements by incrementing the pointer value.

Pointer to arrays in c.

#include <stdio.h>
int main(int argc, char const *argv[])
{
	int array[5]={1,2,3,4,5};
	int *array_pointer=array;
	printf("Accessing array elements using pointer to array\n");
	for( int i=0 ;i<5  ; i++ )
	{
		printf("%d, ",(*array_pointer));
		array_pointer++;
	}
	return 0;
}

Passing Array to a Function

You can pass an array to a function by function parameters. You can also pass the pointer to the array. Here array need not to be static. There is no necesity to mention size in parameter as

int my_function( int array[ ] )

Example for mentioning array size

int my_function( int array[ ] )

Example for passing an array through pointer

int my_function( int *array )

Program to demonstrate passing array to function

#include<stdio.h>
void my_function( int n, int array[] )
{
	printf("Array elements printed from  function\n");
	for( int i=0 ; i<5 ; i++ )
	{
		printf("%d, ",array[i]=i);
	}
}
int main(int argc, char const *argv[])
{
	int array[5];
	for( int i=0 ; i<5 ; i++ )
	{
		array[i]=i;
	}
	my_function(5,array);
	return 0;
}
Passing array to a function

Return an Array from Function

You can return an array from a function.

In c programming it does not allows to return whole array rather returning whole array you can simply return the pointer to array. And c dones not allows to retuen a local array pointer hence you shoud declare the array variable as static.

If you are returning a single dimensional array then you have to declare like shown below:

Syntax

return_type * funtion_name() {
   .
   .
   .
 return array_name;
}

Example program for Return an array from function

#include<stdio.h>
int * my_function(  )
{
	static int array[5];
	for( int i=0 ; i<5 ; i++ )
	{
		array[i]=i;
	}
	return array;
}
int main(int argc, char const *argv[])
{
	int *ptr=my_function(  );
	for( int i=0 ; i<5 ; i++ )
	{
		printf("%d, ",*ptr);
		ptr++;
	}
	return 0;
}
Return an array from function