1

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

1
  • 2
    The problem is that you're trying to compile C code with a C++ compiler. They are different languages, hence the errors. Sep 14, 2010 at 9:34

4 Answers 4

4

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.

5
  • 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). Sep 14, 2010 at 9:16
  • 1
    Sorry, my bad. VS2008 has a C89 mode, /TC - which is the default for .C files.
    – MSalters
    Sep 14, 2010 at 9:22
  • 2
    @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++. Sep 14, 2010 at 9:28
  • @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. Sep 14, 2010 at 9:40
  • 1
    @Konrad: VC has, TTBOMK, only very little C99 support, but it does compile valid C89 code. Arguably, this makes it a C compiler.
    – sbi
    Sep 14, 2010 at 9:48
4

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.)

3
  • 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
    Sep 14, 2010 at 9:50
  • @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. Sep 14, 2010 at 10:00
  • 1
    Quote from the C Standard ( 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
    Sep 14, 2010 at 11:11
2

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 ;
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.