2

I am observing error "warning C4090: 'function' : different 'const' qualifiers " because of below line of code. Going through other similar questions on SO I understand (not 100 percent) it is because of

--> const char* EmployeeList[] and my declaration in qsort of EmployeeList

    #define Elements(array) (sizeof(array)/sizeof((array)[0]))

   const char *EmployeeList[] =
   {
      "Larry Page", "Sergy Brin", "Sundar Pichai", "Merrisa Mayer"
   };

// called from main
SortEmployee(EmployeeList, Elements(EmployeeList));

int Compare(const void *elemA, const void *elemB)
{
 ...
}

void SortEmployee(const char *EmployeeList[], size_t EmployeeCount)
{
    qsort(EmployeeList, EmployeeCount, sizeof(EmployeeList[0]), Compare);
}

However I am unable to resolve it- Any pointers how to do it for array of strings.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
oneday
  • 629
  • 1
  • 9
  • 32
  • I have a feeling that the problem is with the `Compare` function, not the `EmployeeList` array. Posting a [Minimal Complete Verifiable Example](http://stackoverflow.com/help/mcve) would make this question a lot easier to answer. – user3386109 Aug 12 '15 at 19:59
  • I have modified the code part and put much more relevant part of code. if Its not sufficient - will put the entire code - just trying to avoid cluster. – oneday Aug 12 '15 at 20:06
  • I see no problems with that code, the `Compare` function looks correct. I filled in the blanks to make an MCVE, and it compiles without error or warning (using clang with -Wall), and runs fine. Which compiler are you using? – user3386109 Aug 12 '15 at 20:25
  • I am using microsoft compiler – oneday Aug 12 '15 at 20:36

1 Answers1

9

The problem is qsort does not declare its argument as const, while your code does. That means qsort may (in theory) change data, pointed by EmployeeList. So, the compiler reports this error.

Here is the official example: https://msdn.microsoft.com/en-us/library/k77bkb8d.aspx

How ever, here is a simple version to demonstrate my idea:

void foo(char* a) {
   *a = '1'; // I got pointer to char, and changed this char!
}


int main() {
   const char *a = "A"; // I have "CONSTANT POINTER": it points to CONSTANT memory, that CAN NOT be changed (by the way, string constants can't in many environments).
   foo(a); // I pass it to my function, that will change it.
   return 0;
}

Image your compiler stores a in read-only memory (It can, because we told it "this is a pointer to READ ONLY data"). You then modify it (in main function). Something bad may happen. So, the compiler warns you "hey, you pass a pointer to constant data to some function, that does not know that this data is constant and may change it"

fivef
  • 2,397
  • 21
  • 21
user996142
  • 2,753
  • 3
  • 29
  • 48
  • 1
    Of course it will change data pointed by `EmployeeList` : it's the pointers it's supposed to sort. The `const` only applies to the next level of dereferencing. – Quentin Aug 12 '15 at 20:18
  • @user996142 - Thanks for the detailed answer. I do understand that and I tried to point out that problem in my question - I was wondering is there a way to overcome it - assuming I cannot change const char *EmployeeList[] = { "Larry Page", "Sergy Brin", "Sundar Pichai", "Merrisa Mayer" }; – oneday Aug 12 '15 at 20:34
  • I just changed void SortEmployee(const char *EmployeeList[], size_t EmployeeCount) to void SortEmployee(char *EmployeeList[], size_t EmployeeCount) and I no longer see it - Thanks for the help !! – oneday Aug 12 '15 at 20:36