tags:

views:

100

answers:

5
+1  Q: 

sort of arguments

i have following code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int comp(const void *a, const void *b){
char *const *aa=a;
char *const *bb=b;
return strcmp(*aa,*bb):


}
int main(int argc,char **argv){

int i;
  qsort(argv+1,argc-1,sizeof *argv,comp);
   for (i=1;i<argc;i++)
       printf("i: %d==> '%s'\n",i,argv[i]);
   return 0;


}

but here is errors

Error   1   error C2440: 'initializing' : cannot convert from 'const void *' to 'char *const *' c:\users\student\documents\visual studio 2008\projects\sort_argv\sort_argv\sort_argv.cpp    5   sort_argv
Error   2   error C2440: 'initializing' : cannot convert from 'const void *' to 'char *const *' c:\users\student\documents\visual studio 2008\projects\sort_argv\sort_argv\sort_argv.cpp    6   sort_argv

Error 3 error C2143: syntax error : missing ';' before ':'

c:\users\student\documents\visual studio 2008\projects\sort_argv\sort_argv\sort_argv.cpp    7   sort_argv

Error 4 error C2143: syntax error : missing ';' before ':'

c:\users\student\documents\visual studio 2008\projects\sort_argv\sort_argv\sort_argv.cpp    7   sort_argv

please help

+2  A: 

I think your question is not well asked, and it shows that you didn't even read your code carefully before asking. However, one obvious mistake is that:

return strcmp(*aa,*bb):  // : instead of ;
Cedric H.
+1  A: 

How do I sort the elements of argv in C?

Sjoerd
Hmmm ... I'll be more careful in future :)
pmg
+4  A: 

Well, are you using C or C++ ? You've got a valid C program (minus the typo : instead of ;). Yet you name it ".cpp", and initially tagged the question only "C++" (fixed now). If you don't tell it otherwise, Visual Studio's compiler will compile ".cpp" files as C++. And your program is not valid C++.

In C++, you would use std::sort, not qsort. It has a different interface, and const void* is not needed. Then your problem will disappear.

MSalters
OP is using Visual Studio, not GCC. And Visual Studio doesn’t ship with a C compiler at all! It only knows C++. All you can do is switch a project setting that will disallow some C++ features to simulate the C subset of C++ (And perhaps allow more lenient pointer conversions? Dunno).
Konrad Rudolph
Sorry, my bad. VS2008 has a C89 mode, `/TC` - which is the default for `.C` files.
MSalters
@Konrad: That is incorrect. The MSVC compiler is both a C and a C++ compiler and it uses compiler switches and/or the source file extension to determine which language to compile. As the OP found out, there are C constructs that are not valid in C++.
Bart van Ingen Schenau
@Bart, @MSalters: I can’t find my references now so I’m withdrawing my statement but I’m pretty sure that the “C compiler” in VC++ isn’t actually one, it’s just a stripped-down version of the C++ compiler. At the very least, it doesn’t support a C standard that’s more recent than C89.
Konrad Rudolph
@Konrad: VC has, TTBOMK, only very little C99 support, but it does compile valid C89 code. Arguably, this makes it a C compiler.
sbi
A: 

Try replacing:

int comp(const void *a, const void *b){
    char *const *aa=a;
    char *const *bb=b;
    return strcmp(*aa,*bb):
}

with:

int comp(const void *a, const void *b){
    const char* aa = (const char*)a;
    const char* bb= (const char*)b;
    return strcmp(*aa,*bb):
}

..to solve the pointer conversion problem.

Jon Cage
+4  A: 

In C++, use sort instead of qsort. Furthermore, the elements of argv must not be mutated since they are (implicitly, for C backwards compatibility) declared const. Therefore, you need to copy them somewhere else: (Apparently not true.)

#include <string>
#include <vector>
#include <algorithm> // for sort

int main(int argc, char const* argv[]) {
    std::vector<std::string> args(argv, argv + argc);
    std::sort(args.begin(), args.end());
}

(Notice that this solution uses string and vector instead of C strings and C-style arrays where possible. That’s not necessary but it makes the code much easier and shorter.)

Konrad Rudolph
Why can't `argv` be modified? It certainly can't be for backwards compatibility with C: the C89 and C99 standards explicitly allow modifying `argv` (section 5.1.2.2.1/2 in the C99 standard). I can't find anything in the C++98 standard, but I don't see anything about it in the "C compatibility" section either.
jamesdlin
@jamesdlin: I think I may just have been assuming that without ever looking it up, since C++ allows declaring `argv` as `char const*[]` (or does it?), and since C string literals are implicitly `const`. Thanks for pointing out that flaw.
Konrad Rudolph
Quote from the `C` Standard ( http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/n869.txt.gz ) n869.txt 5.1.2.2.1 (the emphasis is mine): "The parameters argc and argv and the strings pointed to by the argv array **shall be modifiable by the program**, and retain their last-stored values between program startup and program termination."
pmg