Array Subscript in C with Examples

This C Tutorial Explains Array Subscript in C Language with Example(s).

An array subscript in C is an integer type constant or variable name with a value ranging from 0 to SIZE 1. (Where size is the total no of elements in the array).

Let’s start with what we already know by considering the declaration of a pointer-to-integer given below,

    int banks = 100;	/* banks, an integer */
    int *pi = &banks;	/* pointer 'pi' points to integer 'banks' */
 
    printf("value of banks is %3d\n", banks);
    printf("value of banks, using ptr-to-int, pi, is %3d\n", *pi);

Here, we performed indirection on pointer-to-integer ‘pi’ to access the location ‘pi’ is pointing to and that is integer banks, reading value there as R-Value or writing new value over there as L-Value.

Let’s, now, try a one-dimensional array, for example,

    int bottles[5] = {23, 34, 11, 56, 12};

This time we analyse every aspect of declaration and initialization of array bottles[5]. At first, block of memory is allocated to the array before initialization begins. There are 5 initializers in the list which are assigned one each into locations beginning with indices 0 through 4. This initialization takes place as below,

advertisement
advertisement
    bottles[0] = 23;
    bottles[1] = 34;
    bottles[2] = 11;
    bottles[3] = 56;
    bottles[4] = 12;

Now, array ‘bottles[5]’ is initialized with values from the initializer’s list. Let’s now come to see how can we access the values in the array. We consider, for example, first element of the array,

    bottles[0];    /* what's this? */

Remember that name of an array is a Pointer Constant i.e. some constant address in memory i.e. this can’t be changed or modified so is constant! And this pointer constant points to the beginning of the array, more clearly to the first element of the array which is ‘bottles[0]’. As we know we dereference the pointer-to-integer to access the integer indirectly using the pointer. Similarly, to access the value, an integer, at the location ‘bottles[0]’ pointed to by ‘bottles’, ‘bottles’ being a pointer constant, we need to perform indirection on the pointer constant i.e. on ‘bottles[0]’. Here, we have subscript operator, ‘[]’, which aside from having higher precedence than indirection operator (*) is same as indirection operator. So, subscripts in arrays perform indirection or dereferencing. This indirection as in pointers causes value to read from the pointed to location as R-Value or write new value there as L-Value.

Therefore, expression,

    bottles[0]

reads value 23. Now, we move over to next location in the array bottles

    bottles[1]	/* what's this" */

As we know an integer occupies 4 bytes on Linux System and array is allocated block of memory in continuous locations, we compute the address for 2nd element

advertisement
    bottles[1]

in order to evaluate its value. Let’s see how? Firstly, name of array, bottles, gives the address of first element, let’s say,

    1000

then index 1, multiplying by 4, is scaled to the size of integer because bottles is an array of integers.

advertisement
    1 * 4

This value is added to 1000, where array points to in memory.

    1000 + 1 * 4

and then subscript performs indirection on the resultant address value, here 1004 to access the value at pointed to location. This process can be repeated for other elements with their specified index value in the array.

Because subscript ‘[]’ operator performs the same function as indirection operator ‘*’ with arrays, we can substitute the expression:

    bottles[0];

with

    *(bottles + 0);

which can be reduced to

    *(bottles);

meaning that when indirection is performed on array name this results in value of the first element.

Similarly, we can write

    bottles[1] as *(bottles + 1)

and so on.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.