0

I'm working on Chapter 8 Challenge 3 from C Programming for the Absolute Beginner 2nd Edition. The program is supposed to sort an array of names alphabetically.

My program doesn't work. The main function without sort() works, but the sort function is messed up; also strcmp() seems to be used incorrectly, based on warning messages.

The compiler I'm using is gcc and I wrote the code with nano.

/* Uses strcmp() in a different function
   to sort a list of names in alphabetical order */

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

void sort(char*, int);

void main() {
    char strStates[4][11] = { "Florida", "Oregon", "California", "Georgia" };
    sort(*strStates, 4); // 4 is the number of string in the array

    printf("\nFour States Listed in Alphabetical Order\n");

    int x;
    for(x = 0; x < 4; x++)
        printf("\n%s", strStates[x]);
}

void sort(char* strNames, int iStrings) {
    char strPlaceholder[11] = { 0 };
    int x;
    int y;

    for(x = 0; x < iStrings; x++) {
        for(y = 0; y < iStrings - 1; y++) {
            if(strcmp(strNames[y], strNames[y + 1]) > 0) {
                strcpy(strPlaceholder, strNames[y + 1]);
                strcpy(strNames[y + 1], strNames[y]);
                strcpy(strNames[y], strPlaceholder);
            }
        }
    }
}
Akira
  • 4,385
  • 3
  • 24
  • 46
dexter27
  • 3
  • 1
  • Please, put your code in – FieryCat Jun 09 '17 at 05:59
  • 5
    Please include your program text directly in the question. Copy'n'paste the code into the edit box. Make sure it looks OK, ignoring the previewl Then select the code and use the **`{}`** button above the box to indent it. Now check the preview. Make sure there's no tabs in the code you copy; that will wreck the layout when you indent it. – Jonathan Leffler Jun 09 '17 at 06:00
  • The argument type of `sort` is incorrect. – BLUEPIXY Jun 09 '17 at 06:05
  • 2
    Think about it. Are we supposed to manually retype your code to try it? Paste the text! Include your full error messages, too. – Mark Tolonen Jun 09 '17 at 06:06
  • 1
    @MarkTolonen: don't include the image — mark the question for closure "unclear what you're asking". Don't encourage people to leave images. All else apart, it isn't possible to copy'n'paste the code to test it, or see what it really says. – Jonathan Leffler Jun 09 '17 at 06:18
  • Sorting logic is wrong. you should fix it first. – Kanji Viroja Jun 09 '17 at 06:18
  • @JonathanLeffler Most people with coming to this question are going to click the link. Why not make it visible without the extra hassle? The OP was already prompted to fix it. Give the OP a chance to. – Mark Tolonen Jun 09 '17 at 06:27
  • 1
    They've had half an hour — that's long enough. I disagree, but I won't undo any subsequent attempt to insert the image. I think inserting it lets the OP off the hook when they should not be let off the hook — but I'll bow to the collective preference of the denizens of SO. – Jonathan Leffler Jun 09 '17 at 06:30
  • [Hint](https://stackoverflow.com/a/31016254/971127) – BLUEPIXY Jun 09 '17 at 09:04

1 Answers1

0

Not meant as an answer but as a hint to let you get forward. A two dimensional array like char[4][11] is something different than a pointer to a (sequences of) characters like char*.

Suppose the following code:

char *s = "Florida"; // lets pointer 's' point to a sequence of characters, i.e. { 'F', 'l', 'o', 'r', 'i', 'd', 'a', '\0' }
char arr[2][11] = { "Florida", "New York" };

Then an expression like s[1] is equivalent to *(s + sizeof(char)), which is *(s+1), whereas an expression like arr[1] is equivalent to *(arr + sizeof(char[11])), which is *(arr + 11), not *(arr + 1). The "sizeof"-part is done by the compiler and is derived from the type of the variable. Hence an argument of type char* behaves different than an argument of type char[11].

The following code might help you forward:

void print (char array[][11], int n) {

    for(int i=0;i<n;i++)
        printf("%d:%s\n",i,array[i]);
}

int main() {

    char strStates[4][11] = { "aer", "adf", "awer", "aert" };
    print (strStates,4);

    return 0;
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58