Simple program to shuffle an array of strings in C

Here’s a quick and simple program for shuffling an array of strings in C. There are many ways to do this, but I needed something much simpler and more beginner-friendly (ie: complete) than the examples I was finding online.

Enjoy!

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

#define CITY_NAMES_COUNT 10

const char* cityNames[] = {
    "Seattle",
    "Chicago",
    "San Diego",
    "Orlando",
    "Anahiem",
    "Tampa",
    "Indianapolis",
    "Portland",
    "Chelan",
    "Sandusky"
};

void shuffleCities()
{
    int size = CITY_NAMES_COUNT;
    if (size > 1)
    {
        int i;
        //step through each index of the city name array
        for (i = 0; i < size - 1; i++)
        {
        //pick a random index (j) to swap it with
        //okay to pick same value as i
        int j = rand() % CITY_NAMES_COUNT; //random between 0 and 10
        const char* temp = cityNames[j];
        cityNames[j] = cityNames[i];
        cityNames[i] = temp;
        }
    }
}

int main(int argc, char* argv[])
{
    srand (time(NULL));
    shuffleCities();

    printf("SHUFFLED CITY NAME ARRAY\n");
    int p;
    for (p = 0; p < CITY_NAMES_COUNT; p++) {
        printf("%i: %s\n", p, cityNames[p]);
    }
}

To compile:

 gcc -o shuffleCities shuffleCities.c

To run:

./shuffleCities

Expected output:

SHUFFLED CITY NAME ARRAY
0: Portland
1: Tampa
2: Sandusky
3: Seattle
4: Chelan
5: San Diego
6: Anahiem
7: Chicago
8: Indianapolis
9: Orlando

Passing a 2D array to a function in C++ as an array or pointer

I recently had an opportunity to experience the joys of passing 2D arrays (both as themselves and as pointers) to functions in C++.

Both of my examples here assume you already know the size of your 2D array (3×3, in this case).

int array[3][3] = {{0,0,0},{0,0,0},{0,0,0}};

Passing a 2D array to a function in C++

Notice the empty first set of brackets. For some reason, C++ only needs to know the columns. It can figure out the rows on its own (though it won’t break if you do include the row size).

int array[3][3];

void doStuff(int arr[][3]) {
   //accessing stuff inside it
   int someCellData = arr[0][0];
}

doStuff(array);

Passing a pointer to a 2D array to a function in C++

array[3][3];
int (*p_array)[3][3] = &array;

void doStuff(int (*arr)[3][3]) {
    //accessing stuff inside it
    int someCellData = (*arr)[0][0];
}
doStuff(p_array); //notice you pass it without the *

Additional notes on passing a pointer to a 2D array to a function in C++

When you declare and define the pointer to the array, you have to include the rows and columns sizes in accompanying square brackets.

If you don’t, you’ll get this error on that pointer’s definition line when you compile:

cannot initialize a variable of type 'int (*)' with an rvalue of type 'int (*)[3][3]'

So, don’t define your pointer to your 2D array like this:

int (*p_array) = &array; //wrong!

Do it like this:

int (*p_array)[3][3] = &array; //include rows & columns

More reading

These Stack Overflow queries helped me figure this out: