Thread: Finding a hyphen in an array.

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Finding a hyphen in an array.

    I'm trying to find out how to read a hyphen from an array. I've got the following set up:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char data[5];
    	int a = 0;
    	puts("Enter: ");
    	fgets(data, 5, stdin);
    	for (;a < 5 ; a++)
    	{
    		if (data[a] = '-')
    		{
    			printf("A - was found!\n");
    		}
    	}
    	return 0;
    }
    It doesn't work however. I get the following output:
    Code:
    pim@zwart:~/C$ ./a-b
    Enter:
    abcde
    A - was found!
    A - was found!
    A - was found!
    A - was found!
    A - was found!
    Obviously the if statement isn't working properly. I wonder why this is the case tough. It's checking for a characters in a character array.

    Anyone has any thoughts on this?

    Thank You.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use ==, not =

    Or use the strchr() function.
    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
    Dec 2007
    Posts
    2,675
    A good habit to get into when comparing a variable against a constant is to use the constant as the left side of the equivalence test. That way, when this happens (as it invariably does to just about everyone) you will get a compile error because you cannot assign a new value to a constant. This saves you from what may be a subtle, hard-to-find bug.

    For example, this code:
    Code:
    #include <stdio.h>
    int main(int argc, char **argv)
    {
       const char hyphen = '=';
       if (hyphen = argv[0])
       {
          printf("Found a hyphen\n");
       }
       return 0;
    }
    generates the following error and warning:
    Code:
    me@box:~> gcc -g -o wtf wtf.c
    wtf.c: In function ‘main’:
    wtf.c:7: error: assignment of read-only variable ‘hyphen’
    wtf.c:7: warning: assignment makes integer from pointer without a cast

  4. #4
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well I don't really understand what you're doing there rags_to_riches.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I believe gcc gives you a warning when you have if (something = something_const).
    Like if (i = 1)
    Though, maybe you need the -Wall (all warnings) flag

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I'm simply demonstrating how to make it easier to catch this common error prior to runtime.

    Another example. You want to compare a variable to the number 42. You make this mistake:
    Code:
    if (x = 42) { ...
    which will compile and run just fine, but instead of comparing 42 to x, you've assigned 42 to x.

    If you put the 42 first, it won't compile, because 42 is a constant, and you can't assign something to a constant.

    Compiling with -Wall produces just one additional warning:
    Code:
     warning: suggest parentheses around assignment used as truth value

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by C_ntua View Post
    I believe gcc gives you a warning when you have if (something = something_const).
    Like if (i = 1)
    Though, maybe you need the -Wall (all warnings) flag
    You have it the exact wrong way round.
    Code:
    if (i = 1)
    Will compile fine and without any problems. You should compare values with if (const_value = = variable). That way when you accidentally type a single = the compiler will barf, as you cannot assign a variable to a constant. The constant expression doesn't have to be a literal, such as 'a' or 2, but can also be a const variable as rags_to_riches showed.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    As rags_to_riches notes, the compiler always evaluates the value on the right hand side (if possible) and assigns that value to the left. So therefore, 42 = x is invalid as x will not always equal 42 and 42 is constant and cannot change. the variable x can change as it contains the value not and is not the value itself. Fundamental concept.
    Anyway try to always use strcmp, it is put there due to == comparing pointers not the actual value of the string. It was created for a reason, use it!
    Also a constantant variable is created with #define, as the compiler goes through it changes these to its value before execution.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding array dimensions
    By deviousdexter in forum C# Programming
    Replies: 3
    Last Post: 11-12-2008, 10:34 AM
  2. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  3. Finding largest element in array
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 09:18 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM