Messing with Array Subscripts

Array notation involves the array’s name followed by a set of square brackets. Within these brackets is a reference to the array element’s number. It’s important to remember that the first element is zero. But the subscript need not always be a constant integer.

Yes, I know: When the brackets are empty, they refer to the array as a whole: a[]. Technically, this notation refers to the array’s base, though you can’t use a[] as a replacement for the first element individually as it’s not a single variable. Otherwise, what lies betwixt the brackets is an expression representing an array element.

Beginners code C using constants in the brackets: a[0], a[1], and so on. If you’re a programmer who mutters as you code, note that a[0] is pronounced “a sub zero.” The value between the brackets is the subscript.

You can also use variables: a[i], which is muttered as, “a sub I.” In this instance, the value of variable i references an element.

The subscript can be any valid expression in C the results in an integer. For example:

a[i+2]

The element referenced is the result of the value of variable i plus two. Or you could do math with two separate variables:

a[x/y]

The element referenced is the value of variable x divided by variable y. The result is rounded to the nearest integer, which isn’t precise so this example is iffy.

How about this:

a[funct()]

Yes, you can use the return value from a function between the brackets, providing that the function returns an integer value. Functions are considered expressions in this manner.

Here is code I threw together to show the various ways you can mess with an array’s subscript:

2024_05_11-Lesson.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int a[5] = { 10, 20, 30, 40, 50 };

    printf("%d\n",a[0]);
    printf("%d\n",a[0+1]);
    printf("%d\n",a[0+1+1]);
    printf("%d\n",a[0+1+1+1]);
    printf("%d\n",a[0+1+1+1+1]);

    int x = 30;
    int y = 10;
    printf("%d\n",a[x/y]);

    srand( (unsigned)time(NULL) );
    printf("%d\n",a[rand()%5]);

    return 0;
}

Array a[] is an integer array with five values, elements zero through four.

The first few printf() statements use addition to output each subsequent element. In a way, this format relates to how a pointer references offsets in a memory buffer, as in: *(a+1)

The various addition examples are a bit silly as they involve too much typing; a programmer would simplify the math. Here I’m showing that you don’t need to simplify as the expression is evaluated by the compiler.

The next printf() statement divides two variables, x and y, to obtain the element number.

Finally, the rand() function returns a random value, which is modulo-ed to an integer between zero and five. This approach is perfectly legit.

The code builds and the program runs. Here’s sample output:

10
20
30
40
50
40
50

I don’t expect anyone to use such subscripts deliberately, but these options demonstrate what’s possible. And if you’re into obfuscation, they provide yet another vehicle to add a bit of mirth and magic to your C source code.

Also see this post.

4 thoughts on “Messing with Array Subscripts

  1. Like you said you can’t do anything with a[] but a is a pointer so can be used as such, eg.

    printf(“%d\n”, *a); // 10
    printf(“%d\n”, *a + 1); // 11 NOT 20 as the +1 is done after *a
    printf(“%d\n”, *(a+1)); // 20
    printf(“%p\n”, a); // address in hexadecimal, eg 0x7ffea38c669

    In C “can” and “should” are two completely different things but there are probably uses for stuff like this.

  2. Be careful mixing the two. Storage is handled differently by the compiler. Consider the difference between:

    char *string = "Hello!";

    And:

    char string[] = "Hello!";

    These are stored in different locations, which is why modifying the first example can lead to trouble.

  3. Tried it. Got segmentation fault 🙁

    I still can’t think of a use for the snippets above that’s better or more convenient than the usual way, but there must be some because I want there to be!

  4. That’s the kind of thing I would mess with eternally until I got it work – or gave up.

Leave a Reply