Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve the problem incompatible pointer types passing 'char (*)[64]' to parameter of type 'const char **'

I kept getting incompatible pointer types passing 'char (*)[64]' to parameter of type 'const char **' when I pass the name[100][64] in the sort function and end up getting segmentation fault 11

#define size 100
#define type 10
// Defining comparator function as per the requirement
static int myCompare(const void* a, const void* b){
    return strcmp(*(const char**)a, *(const char**)b);
}
void sort(int n, const char* arr){
    qsort(arr, n, sizeof(const char*), myCompare);
}
int main(){
    int num; scanf("%d", &num);
    char name[100][64];
    //input
    for(int i = 0; i < num; i++){
        scanf("%s", name[i]);
    }
    //sort
    sort(num, &name[0]);
}

I have tried char* name[100]; to solve the problem, but still get the warning – segmentation fault 11

char* name[100];
//input
for(int i = 0; i < num; i++){
   scanf("%s", name[i]);
}
sort(num, name);

I want to know how to input several strings and sort them like a dictionary.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You declared a character array like

char name[100][64];

used in expressions like a function argument it is implicitly converted to a pointer to its first element of the type char ( * )[64].

Or if to use this call

sort(num, &name[0]);

then again the expression &name[0] that is equivalent to the expression name used as an argument has the type char ( * )[64]

However the function sort where the array is used as an argument has type of the corresponding parameter const char **.

void sort(int n, const char* arr){

There is no implicit conversion between the pointer types. And moreover if even to use an explicit casting nevertheless the call will be incorrect because the argument will mean that you are passing an array of pointers. But the function qsort will need to swap elements of the type char[64] instead of elements of the type const char *.

Your functions can look the following way as shown in the demonstration program below.

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

#define size 100
#define type 10

static int myCompare( const void *a, const void *b ) {
    return strcmp( *( const char ( * )[type] )a, *( const char ( * )[type] )b );
}
void sort( char ( *arr )[type], size_t n  ) {
    qsort( arr, n, sizeof( *arr ), myCompare );
}

int main( void )
{
    char name[size][type] =
    {
        "9", "8", "7", "6", "5", "4", "3", "2", "1", "0"
    };

    size_t num = 10;

    sort( name, num );

    for (size_t i = 0; i < num; i++)
    {
        printf( "%s ", name[i] );
    }

    putchar( '\n' );
}

The program output is

0 1 2 3 4 5 6 7 8 9
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading