Not clear on first one, since what I've found seems sort is for numbers?
Found something with second one.
Create
int cmpstr(void const a, void const b)
{
char const aa = (char const )a;
char const bb = (char const )b;
return strcmp(aa, bb);
}
and change strcmp option to just cmpstr, but it then gives this output.
style: C-style pointer casting [cstyleCast]
char const aa = (char const )a;
^
style: C-style pointer casting [cstyleCast]
char const bb = (char const )b;
^
So, changes style issue on each qsort to just the two lines in cmpstr.
The cppcheck without --enable=all doesn't show anything, but with --enable=all it does.
The program seems to run fine with the original or with the cmpstr mode.
Just wondering if there is a more correct method to do this .
Thanks for the info. Will have to continue looking.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I must be missing something. I've look at a number of links that say they work, but compiler gives a bunch of error lines?? The old style shell sort works perfectly?
Tried a number of things with various different cmp setups, but none compiled.
Have fedora 37 with latest compiler.
real programs have arrays of 300 to 700 strings, but did a simple example.
Tried some things, and found a solution, but is strange.
sort2 version has cppcheck --enable all report the earlier style message.
sort3 and sort4 don't give error, but strangely all three compiles produce identical binary files after strip??
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int cmpstr(void const a, void const b)
{
// from sort2.cpp
// char const aa = (char const )a;
// char const bb = (char const )b;
// return strcmp(aa, bb);
// from sort3.cpp
// char const aa = static_cast<char const="" *="">(a);
// char const </char>bb = static_cast<char const="" *="">(b);
// return strcmp( aa, bb);
// from sort4.cpp
return strcmp( static_cast<char const="" *="">(a), static_cast<char const="" *="">(b));
}</char></char></char></cstring></cstdlib></cstdio>
This is expected, since the semantics of the program remain the same (it is a style warning after all). std::sort must be able to assign the elements that it sorts.
Example: https://godbolt.org/z/c8hsf3jjE
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for al the info. Search had mentioned something about there error might cause run time error that the compiler wouldn't catch with the Cstyle casting? That's why I was looking for solution. Seems the sort would require complete redo of the C style arrays that are created elsewhere in program. Example seems to require more than what is needed with qsort or the simple old shell sort.
Interesting info. thanks again.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Program has 4 qsort options that give this.
fixzzallbz.cpp:557:42: style: C-style pointer casting [cstyleCast]
qsort(datax, num2, sizeof(datax[0]), (int ()(const void , const void *))strcmp);
Usually find it gives a do you mean option..
Have never had a problem, but if there is a more correct way to do it, would like to update it.
Thanks
A few ideas:
std::sort
with a lambdastatic_cast
on the argumentsreinterpret_cast
onstrcmp
Last edit: CHR 2023-05-01
Not clear on first one, since what I've found seems sort is for numbers?
Found something with second one.
Create
int cmpstr(void const a, void const b)
{
char const aa = (char const )a;
char const bb = (char const )b;
return strcmp(aa, bb);
}
and change strcmp option to just cmpstr, but it then gives this output.
style: C-style pointer casting [cstyleCast]
char const aa = (char const )a;
^
style: C-style pointer casting [cstyleCast]
char const bb = (char const )b;
^
So, changes style issue on each qsort to just the two lines in cmpstr.
The cppcheck without --enable=all doesn't show anything, but with --enable=all it does.
The program seems to run fine with the original or with the cmpstr mode.
Just wondering if there is a more correct method to do this .
Thanks for the info. Will have to continue looking.
Using the
-v
option, the output isThat's why I wrote "use
static_cast
on the arguments".Btw,
std::sort
is a generic sorting algorithm.I must be missing something. I've look at a number of links that say they work, but compiler gives a bunch of error lines?? The old style shell sort works perfectly?
Tried a number of things with various different cmp setups, but none compiled.
Have fedora 37 with latest compiler.
real programs have arrays of 300 to 700 strings, but did a simple example.
#include <cstdio></cstdio>
include <cstdlib></cstdlib>
include <cstring></cstring>
include <algorithm></algorithm>
using namespace std;
bool cmp(const char str1, const char str2){
if(strcmp(str1, str2)<0) return true;
else return false;
}
int main()
{
char temp[20], test[10][20]={ "Xray", "Milk", "Apple", "Orange", "Cat", "Dog", "Tree", "Motorcycle", "Tishawnna"};
int i,j;
for(i=0;i<10;i++) printf("%s\n",test[i]);
// sort(test,test+8,cmp);
// std::sort(std::begin(test), std::end(test));
// std::sort(test, test+8, { return strcmp(lhs,rhs) < 0; });
for(i=0;i<9;i++)
for(j=i+1;j<=9;j++)
if(strcmp(test[i],test[j])>0) { strcpy(temp,test[i]); strcpy(test[i],test[j]); strcpy(test[j],temp); };
for(i=0;i<10;i++) printf("%s\n",test[i]);
return 0;
}
Tried some things, and found a solution, but is strange.
sort2 version has cppcheck --enable all report the earlier style message.
sort3 and sort4 don't give error, but strangely all three compiles produce identical binary files after strip??
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int cmpstr(void const a, void const b)
{
// from sort2.cpp
// char const aa = (char const )a;
// char const bb = (char const )b;
// return strcmp(aa, bb);
// from sort3.cpp
// char const aa = static_cast<char const="" *="">(a);
// char const </char>bb = static_cast<char const="" *="">(b);
// return strcmp( aa, bb);
// from sort4.cpp
return strcmp( static_cast<char const="" *="">(a), static_cast<char const="" *="">(b));
}</char></char></char></cstring></cstdlib></cstdio>
int main()
{
char test[10][20]={ "Xray", "Milk", "Apple", "Orange", "Cat", "Dog", "Tree", "Motorcycle", "Tishawnna"};
int i;
for(i=0;i<10;i++) printf("%s\n",test[i]);
qsort(test, 9, sizeof(test[0]), cmpstr);
for(i=0;i<10;i++) printf("%s\n",test[i]);
return 0;
}
Last edit: Michael Setzer II 2023-05-04
This is expected, since the semantics of the program remain the same (it is a style warning after all).
std::sort
must be able to assign the elements that it sorts.Example: https://godbolt.org/z/c8hsf3jjE
Thanks for al the info. Search had mentioned something about there error might cause run time error that the compiler wouldn't catch with the Cstyle casting? That's why I was looking for solution. Seems the sort would require complete redo of the C style arrays that are created elsewhere in program. Example seems to require more than what is needed with qsort or the simple old shell sort.
Interesting info. thanks again.