ok so as a homework assignment I have to sort a c-string array alphabetically. We have to use pointers and cannot using strings (although strcopy and strcmp can be used). I can sort integers and strings but for the life of me cannot figure this out. I'm pretty sure i'm supposed to use some combination of strcmp and strcopy but i can't figure out where.

#include <iostream>
using namespace std;

// function to return a pointer to the smallest element in a sub-array
char **smallest(char **a, int len) {
  // argument "a" points to the first element of a sub-array within the
  // caller's entire array. The first element of this sub-array is always
  // a[0] within "smallest", even though it's not the first  element
  // of entire array in the caller. "smallest" knows nothing about array 
  // elements before the start of the sub-array pointed to by argument "a".
  // The sub-array elements are effectively re-numbered, starting with 0,
  // within "smallest". Makes the code in smallest much simpler.

   char **small= &a[0];   // "small" points to the smallest element so far in
                       // the sub-array.
  // scan rest of sub-array looking for smallest element.
  for (char i=1; i<len; i++) {  // start on 2nd sub-array element, a[1].
       if (a[i] < *small) // must dereference small to compare values
          // gets here if a[i] is smaller than smallest so far.
          small = &a[i];       // save pointer to this new smaller element.
  // now small points to the smallest element in the sub-array pointed to by "a"

// function to swap 2 elements of an array using pointers to the 2 elements.
// Notice no array indexing needed.
void swap(char **x, char **y) {
    // x and y are pointers to elements to be swapped.
    // If they weren't pointers "swap" could not affect the caller's elements.
    char *temp= *x;
    // notice all pointers are dereferenced.
    *x = *y;
    *y = temp;

void selectionSort(char **array, int n) {
     // array pointer cannot be changed, but data can be changed.
     // n is length of array (cannot be changed).

    char **smallptr;         // points to smallest element of a sub-array
    char **endptr = array+n-1;
    for (char **p= array; p<endptr; p++) {
      // this is iteration "j"

      // Select smallest element in sub-array starting at element array[j]
     int len = (endptr - p) + 1;
     smallptr= smallest(p, len); 
      // Actual arguments to "smallest" (above) specify the sub-array starting 
      // with element, array[j]. The "smallest" function knows 
      // nothing about the array elements before array[j]. Within "smallest"
      // the sub-array elements are effectively re-indexed to start with 0.

      // Swap smallest element in sub-array with first element in
      // sub-array. i.e. element array[j] of entire array.
     swap(smallptr, p);


int main() {
   const int len=5;
   char *strings[len] = {"mouse","cat","dog","zebra","ant"};
   selectionSort(strings,len);  // sort into alphabetical order
   // print out the words, now in alphabetical order
   for (int i=0;i<len;i++)
       cout<<strings[i]<<" ";


The program runs it just doesn't change the string at all. Any help is greatly appreciated.

You know what, nevermind. I just figured it out almost as soon as i posted this lol.

in the smallest: if(strcmp(*small, a[i]) > 0) instead of if (a[i] < *small)

fixed everything. Thanks anyway guys

For fixing your own problem. It's a good skill to have. :)
This article has been dead for over six months. Start a new discussion instead.