Problem with isalpha

This is a discussion on Problem with isalpha within the C Programming forums, part of the General Programming Boards category; I have a doubt about the isalpha function The literature says that it accepts a variable of type char. But ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    6

    Problem with isalpha

    I have a doubt about the isalpha function
    The literature says that it accepts a variable of type char.
    But I sent a pointer to a char
    This is what I did
    Code:
    if(isalpha(*(argv+(temp_counter))!=0)
    {
          printf("The string starts with a letter\n");
          printf("The first letter is %c\n",*(argv+(temp_counter));
    }
    It seems to compile and run fine on my comp. I am ussing gcc to compile it. But when I sent it to a friend it did not compile on his comp.
    Any idea why this might be happening.
    Thanks in advance.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    argv is of type char**. You gotta dereference it twice.

    Code:
    		if(isalpha(*(*argv + temp_counter)))
    		{
    			printf("%c", *(*argv + temp_counter));
    		}
    First dereferencing in inner-most parentheses takes argv[0] (path to your program) which is a char*, and then you dereference that again at the position of the temp_counter.
    Last edited by Tonto; 09-25-2005 at 11:03 PM.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    6
    opps I am very sorry about that.I made a mistake when copying. This is what I used
    Code:
     
    if(isalpha(**(argv+(temp_counter))!=0)
    So I have used a pointer to a pointer which points to the first char in the string.
    Using the above an error still occurs.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    You could stop with the pointer arithmetic and just go with the subscript equivalent to make it more obvious which way you're going.

    Code:
    // first character of every argument
    if ( isalpha ( argv[temp_counter][0] ) )
    
    // all characters of argument 0
    if ( isalpha ( argv[0][temp_counter] ) )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome, problem with isalpha
    By Kyeong in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 12:28 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21