Rotating an Array of Strings

Last month, I posted about rotating values in an array. This post used integers, but the question has come to me about doing the same thing with an array of strings. Yes, it’s possible — and just as easy.

An array of strings looks like this:

char *a[] = { "alfa", "bravo", "charlie",
    "delta", "echo", "foxtrot", "golf",
    "hotel"
};

It could look like this:

char a[8][8] = { "alfa", "bravo", "charlie",
    "delta", "echo", "foxtrot", "golf",
    "hotel"
};

As a two-dimensional array, the first dimension is the number of strings, eight. The second dimension represents the size of each string, the maximum number of characters required. Above, this value is also eight, which is one character greater than the longest strings, “charlie” and “foxtrot.” This extra character is necessary to store the null character at the end of the string.

The two-dimensional array approach wastes memory as each of the strings dwells in a buffer 8 characters wide. The construction is also ugly. Further, using a two-dimensional array makes manipulating the strings difficult.

With pointer notation, what’s created is an array of memory locations. These addresses reference the locations of the strings elsewhere in memory. So when you want to manipulate this type of array you swap only the address values and not the strings (characters) stored at the address.

The following update to my original code replaces int array a[] with char pointer array a[], listing eight strings. These strings’ positions are rotated in the array:

2025_03_08-Lesson.c

#include <stdio.h>

int main()
{
    char *a[] = { "alfa", "bravo", "charlie",
        "delta", "echo", "foxtrot", "golf",
        "hotel"
    };
    char *save;
    int size,x;

    /* obtain array size */
    size = sizeof(a)/sizeof(a[0]);

    /* output the array */
    for( x=0; x<size; x++ )
        printf(" %s",a[x]);
    putchar('\n');

    /* shuffle to the left */
        /* retain the first element */
    save = a[0];
        /* move over all remaining elements */
    for( x=0; x<size-1; x++ )
        a[x] = a[x+1];
        /* restore the first element to the last pos. */
    a[size-1] = save;

    /* output the array */
    for( x=0; x<size; x++ )
        printf(" %s",a[x]);
    putchar('\n');

    return 0;
}

Most of the code is the same as before; moving pointer variables is as painless as moving integer variables. The key differences are the a[] is a char pointer array, and variable size (which temporarily stores the first element in the array) is now a char pointer to match the array’s data type. The placeholders in the printf() statements have been updated from %d to display decimal integer values to %s to output strings. That’s pretty much it.

Here’s a sample run:

 alfa bravo charlie delta echo foxtrot golf hotel
 bravo charlie delta echo foxtrot golf hotel alfa

These strings were not relocated in memory. Unlike a two-dimensional array, nothing changes as far as the data is concerned. The only things changed are the positions of the pointers in the array. These pointer values aren’t changed or altered, but their order is changed.

This trick is made possible because of pointers in the C language. I hope you can appreciate how useful they can be, even if you do find them frustrating and complex.

I explore how ugly two dimensional arrays can become in next week’s Lesson.

Leave a Reply