-3

When I try to use toupper, the compiler gives me this error: "warning: cast from pointer to integer of different size." and "warning: assignment makes pointer from integer without cast." Why?

for(i=0;i<argc;i++){
  argv[i]=(char)toupper((char)argv[i]);
}
Ricky Yang
  • 23
  • 2

1 Answers1

5

argv is a char**, a pointer to a pointer. Think of it as an array of strings, not a string.

Indexing with [i] removes one level of indirection, so argv[i] is a pointer to a character, a char*.

toupper() is int toupper (int c); - it expects a single character as an integer, not a pointer to a character, but you are passing it a char* when you do argv[i], which you are then trying to cast to a char... a regular old character, which typically has a different size to a pointer.

argc says how many command line arguments there are, and argv[0] is usually the exectuable name. Use strlen to get the length of each argument in argv.

To convert the whole of each argument to upper case:

for (int i = 0; i < argc; i++) {
    size_t len = strlen(argv[i]);
    for (size_t j = 0; j < len; j++) {
        argv[i][j] = (char)toupper((unsigned char)argv[i][j]);
    }
}

You can read about why the argument to toupper() should be cast to unsigned char in Keith Thompson's excellent answer elsewhere on SO

Community
  • 1
  • 1
Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60
  • 1
    The argument to `toupper` should be cast to `unsigned char`, not to `int`. – Keith Thompson Jul 12 '15 at 23:05
  • bear with me.. I am new to C, but how is argv[i][j] possible, when it was initialized with only one dimension? Further, this compiled, however when I ran the program, and gave it "abcdefg", the program returned "?? ?" – Ricky Yang Jul 12 '15 at 23:19
  • @RickyYang What makes you think `argv` was initialised with only one dimension? `argc` is the length of one dimension, essentially, but each `char*` in `argv` has a different length depending on the command line arguments you pass in. – Iskar Jarak Jul 12 '15 at 23:24
  • @RickyYang Take a look at http://ideone.com/oBFGcM `argv[0]` is typically the name of the program invoked, in this case `./prog` - see how the output is capitalised? Unfortunately ideone doesn't let you set command line arguments so you'll only see the default one in the output there. – Iskar Jarak Jul 12 '15 at 23:29
  • might this be different in C? Because the output I am getting is a bunch of spaces and question marks. – Ricky Yang Jul 12 '15 at 23:34
  • @rickyyang Your program is wrong, despite it compiles. Iskars only have the fault of making argv[0] uppercase, otherwise it is what you want. – Mikkel Christiansen Jul 12 '15 at 23:44
  • @RickyYang My code is C. Plain standards compliant C99. What compiler and OS are you using? – Iskar Jarak Jul 12 '15 at 23:52
  • @IskarJarak Except for the int and size_t in the for headers, your code would work in all c versions except some from the 70's. And if argv[0] isn't the name of the program, it is the empty string. – Mikkel Christiansen Jul 13 '15 at 00:00