Thread: Need a few clarifications

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    15

    Need a few clarifications

    Hey guys,
    I'd like to know why:

    1. I can't use a return statement in a ternary expression. For example,
    Code:
    (x==-1?return -1:return x);
    2. I can't define/set a default value for variables in a structure. For example, taking a structure like this,
    Code:
    struct student
    {
    
    
        char name[20];
        int reg_no;
        int math,phy,chem;
        char flag='t';
    
    
    };
    I get an error:
    Code:
    error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
    Thanks in advance for the help!



    Have an awesome day!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    1.
    The operands of the ternary operator (or subexpressions of any expression) must themselves be expressions. Your question is equivalent to "why can't I do this:"
    Code:
      x = y + return 2;
    return is simply not an expression, i.e., it has no value (and wouldn't really have any use for a value anyway since it causes the function to terminate).

    Although you shouldn't use the ternary operator just to avoid an if statement, in this case your expression could be properly written as:
    Code:
      return (x == -1) : -1 : x;
    or, simply
    Code:
      return x;
    2.
    You simply can't do it that way.
    You need to set it explicitly when you use the struct.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    1. You can, but if x == -1, and you return -1, and in other cases you return x, that reduces to return x;
    To be less of a donkey about it, yes, something like this works.
    Code:
    return x == something ? -1 : x;
    2. What you wrote there is a structure declaration. When you define a variable to be your structure type, then you will get the opportunity to make the flag be some value. As far as I'm aware, there is no notion of default values like in C++.

    If you need to be certain that the struct is given certain values, you can use the opaque pointer idiom, and force the programmer to write something like:
    Code:
    struct student *classmate = student_init("John Doe", 123456, 0, 0, 0);
    Then inside the function you can ensure the initial flag value, 't'. You have to design a suite of functions around the structure to use it, but it's worth it in the end in most real-world situations.

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Quote Originally Posted by algorism View Post
    1.
    The operands of the ternary operator (or subexpressions of any expression) must themselves be expressions. Your question is equivalent to "why can't I do this:"
    Code:
      x = y + return 2;
    return is simply not an expression, i.e., it has no value (and wouldn't really have any use for a value anyway since it causes the function to terminate).

    Although you shouldn't use the ternary operator just to avoid an if statement, in this case your expression could be properly written as:
    Code:
      return (x == -1) : -1 : x;
    or, simply
    Code:
      return x;
    2.
    You simply can't do it that way.
    You need to set it explicitly when you use the struct.
    Whoops,
    Actually, that return statement is part of a larger function where I check the input marks for a subject for validity (like marks>=0 and <=100) and if any other improper value is entered, return -1. I apologize if that caused any confusion as to why I'm apparently checking for a specific value of x if I'm gonna return x anyway :P


    Also, your solution worked perfectly—thanks!

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Quote Originally Posted by whiteflags View Post
    1. You can, but if x == -1, and you return -1, and in other cases you return x, that reduces to return x;
    To be less of a donkey about it, yes, something like this works.
    Code:
    return x == something ? -1 : x;
    2. What you wrote there is a structure declaration. When you define a variable to be your structure type, then you will get the opportunity to make the flag be some value. As far as I'm aware, there is no notion of default values like in C++.

    If you need to be certain that the struct is given certain values, you can use the opaque pointer idiom, and force the programmer to write something like:
    Code:
    struct student *classmate = student_init("John Doe", 123456, 0, 0, 0);
    Then inside the function you can ensure the initial flag value, 't'. You have to design a suite of functions around the structure to use it, but it's worth it in the end in most real-world situations.
    Gotcha, opaque pointers seem kinda complex right now. I'll look into it later.
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some clarifications on a code
    By R4meau in forum C Programming
    Replies: 2
    Last Post: 10-12-2015, 07:42 PM
  2. Seeking some clarifications on these codes.
    By Dontgiveup in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 07:20 PM

Tags for this Thread