Home > Back-end >  Using const char to organize strings
Using const char to organize strings

Time:11-15

I'm trying to sort these pre-determined strings alphabetically, but my strcmp and strcpy are saying that my const char* is being violated by char* used in the functions.

Heres the code;

#include <stdio.h>
#include <string.h>
#define size 10

int main(void) {
    const char* cities[size] = { "Acushnet", "Dartmouth", "Fairhaven", 
            "Fall River", "Freetown", "Marion", 
            "Mattapoisett", "New Bedford", "Rochester", "Westport" };
    //using values a and b to hold values
    int a;
    const char b;
    for (int i = 0; i < size; i  ) {
        for (int a = i   1; a <= size; a  ) {
            if (strcmp(cities[i], cities[a]) > 0) {
                strcpy(b, cities[i]);
                strcpy(cities[i], cities[a]);
                strcpy(cities[a], b);
            }
        }
    }
}

CodePudding user response:

strcpy expects a char * for the first argument and a const char * for the second. b is neither of these but a const char instead. Also, because your array members have type const char *, they can't be passed as the first argument to strcpy.

Since you have an array of pointers, you only need to swap the pointers themselves, not what they point to.

        if (strcmp(cities[i], cities[a]) > 0) {
            const char *tmp = cities[i];
            cities[i] = cities[a];
            cities[a] = tmp;
        }

CodePudding user response:

The variable b has the type const char.

const char b;

So this call of the function strcpy

strcpy(b, cities[i]);

does not make a sense because the first argument of the function must have the type char *. What you need is to swap pointers that represent elements of the array of pointers.

Moreover this for loop

for (int a = i   1; a <= size; a  ) {

can result in accessing memory beyond the array cities when a is equal to size because the valid range of indices is [0, size)..

You need to declare the variable b as having the type const char * as the type of elements of the array.

const char *b;

And write the for loops at least the following way

for (int i = 0; i < size; i  ) {
    for ( int a = i   1; a < size; a  ) {
        if ( strcmp(cities[i], cities[a]) > 0) {
            b = cities[i];
            cities[i] = cities[a];
            cities[a] = b;
        }
    }
}

Here is a demonstrative program.

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

int main( void ) 
{
    const char* cities[] = 
    { 
        "Acushnet", "Dartmouth", "Fairhaven", "Fall River", "Freetown", "Marion", 
        "Mattapoisett", "New Bedford", "Rochester", "Westport" 
    };
    
    const size_t size = sizeof( cities ) / sizeof( *cities );
    
    for ( size_t i = 0; i < size; i   )
    {
        for ( size_t j = i   1; j < size; j   )
        {
            if ( strcmp( cities[j], cities[i] ) < 0 )
            {
                const char *tmp = cities[i];
                cities[i] = cities[j];
                cities[j] = tmp;
            }
        }
    }
    
    for ( size_t i = 0; i < size; i   )
    {
        printf( "\"%s\" ", cities[i] );
    }
    putchar( '\n' );
    
    return 0;
}

The program output is

"Acushnet" "Dartmouth" "Fairhaven" "Fall River" "Freetown" "Marion" "Mattapoisett" "New Bedford" "Rochester" "Westport" 
  • Related