Thread: Question

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Question

    I have the following code:
    Code:
    /* The program convert lower case to upper case 
    or upper case to lower case, according to the
    name display in arg[0]*/
    ---------------------------------------------------------------------------------------------------------------
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    int main(int argc, char **argv)
    {
        int(*convcase[2])(int)={toupper,tolower};
        int func;
        int result = EXIT_SUCCESS;
        
        int ch;
        
        if(argc > 0)
        {
                if(toupper((unsigned char)argv[0][0])=='U')
                func=0;
                else
                func=1;
                
        while ((ch=getchar()!=EOF))
        {
              ch(*convcase[func])((unsigned char)ch);
              putchar(ch);
              }
                }
        
        else
        {
            fprintf(stderr,"Unknown name.\n");
            result=EXIT_FAILURE;
        }
        
        return (result);
    }
    ---------------------------------------------------------------------------------------------------------------

    Can someone advise what is the meaning of the following line, I do not understand it:

    if(toupper((unsigned char)argv[0][0])=='U')

    Thanks,
    itit
    Last edited by Salem; 03-12-2011 at 10:31 AM. Reason: Added

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    So where did you copy/paste this code from?

    It's hard to believe that you could come up with an array of function pointers, and then not know what that line is all about.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    of course I have copied it!!! I'm studying it, and do not understand this code segment. Hence, instead of preach me, try to help me (if you can...) or you cannot?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itit View Post
    of course I have copied it!!! I'm studying it, and do not understand this code segment. Hence, instead of preach me, try to help me (if you can...) or you cannot?
    Hint: Scoop and Poop coding is generally frowned upon because, for the most part it fails to educate and tends to cause more problems than it solves. However, in a situation where you have copied code to study it, it's to your best advantage to say so up front. Knowing whether you wrote it or not will change the nature of the answers you get quite significantly.

    Code:
    if(toupper((unsigned char)argv[0][0])=='U')
    This line is converting the first character of the command line to an upper case character then testing if it is a capital U.

    Actually that line most likely contains a mistake since argv[0] is program's command line, argv[1] is the first parameter.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Quote Originally Posted by CommonTater View Post
    Actually that line most likely contains a mistake since argv[0] is program's command line, argv[1] is the first parameter.
    I'm inclined to believe it's intentional. The program will behave differently depending on if it's named "upper" or "lower" for example. I've seen OS commands that do something similar, where a command in /usr/bin has multiple hard links.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Clairvoyant1332 View Post
    I'm inclined to believe it's intentional. The program will behave differently depending on if it's named "upper" or "lower" for example. I've seen OS commands that do something similar, where a command in /usr/bin has multiple hard links.
    You don't see that --at least not in my experience-- on Windows systems... but yes I can see how that would work. More *nix wierdness...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM