Subscribe Us

Array - Definition Declaring Initializing advantage disadvantage

Array - Definition Declaring Initializing advantage disadvantage

I hope,You have already know the basic of C programming language, like Data Type, Variable, Constant, operators, Control Statement, pointer, All the basic concept helps you to understand array concept in C programming easily.

What is Array[] in C programming language?

>> Array is a variable that hold multiple elements which has the same data type. Array has more advantage and disadvantage also.

How to declear an Array [] in a C program ?

>> You can declare an Array[] by specifying its data type, name and the fixed number of elements the array holds between square brackets immediately following the array name. The following illustrates how to declare an array:

data_type array_name[size];

For example, to declare an array that contains 100 integer numbers you can do as follows:

int a[100];

You must follow the rules below when you declare an array in C:

> The data type can be any valid C data types including C structure and union.
> The name of an array has to follow the rule of C variables.
> The size of array has to be a positive constant integer.

How to Initialize an Array[] in C program ?

It is like a variable, an array can be initialized. To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name. Here is an example of initializing an array of integers.

int list[5] = {2,1,3,7,8};

Accessing Array Element

You can access array elements via indexes like array_name[index].
Indexes of array starts from 0 not 1 so the highest elements of an array is array_name[size-1]. let's example, int num[4]={10,20,30,40};

num[0] = 10
num[1]=20
num[2] =30
num[3]=40



Advantages and Disadvantages of Array in C

Advantages

Disadvantages

Array helps to store the data of same datatype with same size. It also store many element at a time.

Any element of an array you can access through index of that particular element.

In array data storing is memory wastage or shortage.

If you want to delete any element from the array, have to traverse throughout the whole array.



Array And Pointer

An array is a pointer to the 0th element of the array. When you dreference the array name you will get the 0th element. This give us the possibility of accessing array's element not only via index but also via pointer. It is note that the array is treated as constant so you can only modify the values in the array but not array itself. The program below will help you visualize the memory address each array's element and how to access array's elements using pointer.

#include
void main()
{ const int size = 5;
int list[size] = {2,1,3,7,8};
int* plist = list;
// print memory address of array elements
for(int i = 0; i < size;i++)
{ printf("list[%d] is in %d\n",i,&list;[i]);
} // accessing array elements using pointer
for(i = 0; i < size;i++)
{ printf("list[%d] = %d\n",i,*plist);
/* increase memory address of pointer so it go to the next
element of the array */
plist++;
}
}

Output ::


list[0] is in 1310568
list[1] is in 1310572
list[2] is in 1310576
list[3] is in 1310580
list[4] is in 1310584
list[0] = 2
list[1] = 1
list[2] = 3
list[3] = 7
list[4] = 8

You can store pointers in an array and in this case we have an array of pointers. The following illustrates an array of pointers.

int *ap[10];

:: Multidimensional Arrays ::

An array with more than one index value is called a multidimensional array. All the arrays you used above are called single-dimensional arrays.
To declare a multidimensional array you can do as follows:

data_type array_name[][][];

The number of square brackets specifies the dimension of the array. For example to declare two dimensions integer array we can do as follows:

int matrix[3][3];

Initializing Multidimensional Arrays

You can initialize an array as a single-dimension array. Here is an example of initializing an two dimensions integer array

int matrix[3][3] =
{ {11,12,13},
{21,22,23},
{32,31,33},
};

Post a Comment

1 Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete