8.2 – Arrays points to remember

by subbu on October 22, 2013

The subscript must be an integer constant:

While declaring an array makes sure that, the subscript must be 0 or a positive integer constant. Variables and other constants like negative integers and real constant can’t be used.

const variables and macros can be used as a subscript in arrays.

int a=5;
int x[a];            /* results error */

Here, variable a is used as a subscript to declare an array x. It results error because compiler has to know the size to allocate the memory while compiling the program that is before executing the program. Where as a is only initialized after the compilation that is while executing.

const int a=5;
int x[a];             /* it's ok  */

Here, a is declared as a constant variable using the keyword const. It is allocated and initialized while compilation. Hence compiler knows the size of array during the compilation.

#define MAX 5
int x[MAX];         /* it's ok   */

Here, the preprocessor replaces the 5 in place of MAX before compilation that is, x[MAX] is replaced with x[5]. Hence compiler knows the size of array during the compilation.
Note: const and #define statements will be covered in later sessions.

Guess which are valid and invalid:

1) int a[12.34];
2) int x[0];
3) float sales[12];
4) float loss[-12];
5) int x[‘A’];
6) int a[2+3];
7) int size=10;
int x[size];
8) #define A  5
int a[A];
9) const int x=5;
int [x];
10) int x[];

Show Answers

1) Invalid        //subscript can’t be a real constant
2) Valid           // 0 size arrays can be defined
3) Valid
4) Invalid        // size can’t be specified in negative
5) Valid           // ASCII value of ‘A’ 65 will be taken as size
6) Valid
7) Invalid        // Normal variable can’t be used as subscript
8) Valid           // Macro can be used as subscript
9) Valid           // const variable can be used as subscript
10) Invalid      //can’ declare an array without size

Bounds checking in C language:

In other languages there is a bound checking while using subscript which is more than the size of array. In C language the element is places outside the array boundaries without giving any error. There are chances that, the data may be overwritten on other data or some times on the program. It may result system to crash or produces unexpected result.

#include<stdio.h>
int main()
{
int a[5];
a[-5]=45;                      /* assigning out of bounds */
a[30]=25;                      /* assigning out of bounds */
printf("%d\n%d",a[-5],a[30]);
return 0;
}

Output:
45
25

Assigning other types to an array:

Though array is a set of elements of similar kind stored in adjacent memory allocations, constants of other types can be assigned. Constants of other types will be converted into array type according to the rules of widening and narrow conversions.

#include<stdio.h>
int main()
{
int a[3];
a[0]=25;
a[1]=12.45;          /* truncates to 12 */
a[2]='A';            /* ASCII value of 'A' 65 is assigned */
printf("%d\n%d\n%d",a[0],a[1],a[2]);
return 0;
}

Output:
25
12
65

sizeof() with arrays:

We know that, sizeof() operator is used to find the size of a variable or type in bytes. The sizeof() operator gives the total size of an array that is the size of each element multiplied by the number of elements.

int a[5];

In Turbo C 3.0 sizeof(a) gives 10 because it is an integer array of 5 elements and takes 2 bytes for each element (5*4)
In MinGW gcc sizeof(a) gives 20 because it is an integer array of 5 elements and takes 4 bytes for each element (5*2)

float x[7];

Here, sizeof(x) gives 28 because it is a float array of 7 elements and takes 4 bytes for each element (7*4)

#include<stdio.h>
int main()
{
int a[5];
float x[7];
printf("Size of a  %d",sizeof(a));
printf("\nSize of x  %d",sizeof(x));
return 0;
}

Output in Turbo C 3.0:
Size of a  10
Size of x  28

Output in MinGw gcc:
Size of a  20
Size of x  28

How to find the length of an array in C language:

In C language, there is no direct way to get how many elements are there in the array (length) . Because all the elements of array have the same size, dividing the total size of the array by the size of any one of the elements gives the number of elements in the array. Here we use element 0 because it is the only element guaranteed to exist, as arrays must have at least one element.

#include<stdio.h>
int main()
{
int a[5],len;
len=sizeof(a)/sizeof(a[0]);
printf("Length of array  %d",len);
return 0;
}

Output:
Length of array 5

Flexibility in accessing elements:

While accessing elements from an array, we can place either index or name of array within the subscript but, can’t be done with the array declaration.

int x[5];        /* correct */
int 5[x];        /* results error */

flexibility in element accessing

#include<stdio.h>
int main()
{
int i;
int a[5];
a[0]=23,a[1]=78,a[2]=10,a[3]=55,a[4]=94;
printf("%d\t%d\t%d\t%d\t%d",0[a],1[a],2[a],3[a],4[a]);
return 0;
}

Output:
23      78      10      55      94

Previous post:

Next post: