Like Tree1Likes
  • 1 Post By AndiPersti

How to use the conditional operator?

This is a discussion on How to use the conditional operator? within the C Programming forums, part of the General Programming Boards category; i know Code: (condition) ? true -clause : false -clause but how do you use an array as the condition, ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    9

    How to use the conditional operator?

    i know
    Code:
    (condition) ? true-clause : false-clause
    but how do you use an array as the condition, how will the code look?

    For example i want to write

    Code:
    string numbers[5] = {"one","two","three","four","five"};
        
        numbers == "one" ? thumb : again;
    thumb and again will replace something else. Don't worry about them.
    how do i say that if the numbers array is representing "one" then it replaces as "thumb", otherwise "again".

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Like this.
    Code:
    numbers[i] = numbers[i] == "one" ? thumb : again;
    Note that numbers cannot represent "one", because it's an array of strings. numbers[i] can.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    numbers[i] == "one"
    won't work because you can't compare strings with the == operator in C. You have to use strcmp().

    Bye, Andreas
    laserlight likes this.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,057
    What does the declaration `string' mean in C, anyway? There is std::string in C++, and there is the string.h header file in C. You probably mean

    Code:
    char *numbers[5] = {"one", "two", "three", "four", "five"};
    As was already mentioned, strcmp() is for comparing strings. However, if you *really* want to compare strings with the == operator, then you must make sure you are comparing objects with the same address:

    Code:
    char *S_ONE = "one";
    char *S_TWO = "two";
    /* ... */
    
    int main(void) {
      char *numbers[5] = {S_ONE, S_TWO, S_THREE, S_FOUR, S_FIVE};
      char *s = numbers[2];
      printf("%s\n", s == S_THREE ? "You got it." : "Nope.");
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Yeah I read string, and assumed it was std::string, not realizing that this is the C forum. In c++ you can compare strings like that, but not C.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Or there's the obvious possiblity that this was posted in the wrong forum.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with conditional operator.
    By One in forum C Programming
    Replies: 2
    Last Post: 12-11-2012, 07:01 AM
  2. Conditional Operator
    By Waqas Asad in forum C Programming
    Replies: 4
    Last Post: 12-19-2011, 08:11 AM
  3. Conditional Operator
    By markcocoa10 in forum C Programming
    Replies: 7
    Last Post: 10-15-2010, 06:22 PM
  4. Conditional Operator
    By arjunajay in forum C Programming
    Replies: 8
    Last Post: 07-10-2008, 08:17 AM
  5. Conditional operator ? :)
    By ER in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2001, 02:34 AM

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