Like Tree1Likes
  • 1 Post By Nominal Animal

lvalue required as left operand of assignment

This is a discussion on lvalue required as left operand of assignment within the C Programming forums, part of the General Programming Boards category; Good evening. Why do I get the error with the ternary operator? Code: #define FOLD 40 int getLine(char* s, int ...

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question lvalue required as left operand of assignment

    Good evening. Why do I get the error with the ternary operator?

    Code:
    #define FOLD 40
    
    int getLine(char* s, int lim)
    { 
       int i;
       char c;
       for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++) 
        (i % FOLD == 0)? s[i] = '\n' : s[i] = c;
      
      if(c == '\n')
         s[i++] = c; 
       
       s[i] = '\0'; 
       return i; 
    }
    if I write

    Code:
    if(i % FOLD == 0) 
     s[i] = '\n'; 
    else 
     s[i] = c;
    I don't get the error. Why?

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    868
    Quote Originally Posted by thames View Post
    Good evening. Why do I get the error with the ternary operator?
    Code:
        (i % FOLD == 0)? s[i] = '\n' : s[i] = c;
    if I write

    Code:
    if(i % FOLD == 0) 
     s[i] = '\n'; 
    else 
     s[i] = c;
    I don't get the error. Why?
    Because the ternary operator is an operator (that evaluates to a value), not a statement (like if is).

    If you want to assign a newline to s[i] when i is a multiple of FOLD, and c otherwise, you can use s[i] = (i % FOLD) ? c : '\n'; or s[i] = (i % FOLD == 0) ? '\n' : c;
    Note how the ternary operator is just an expression, not a statement? How it yields the value you assign?

    That aside, do you realize that by doing that, you replace every FOLDth character by a newline, causing the replaced character to be lost?

    Hint: only increment the index i in the loop definition. If you are at a folding point, append a space to the output array. Otherwise, read a character from input. If the character is EOF or newline, break out of the loop. Otherwise append it to the output array.
    thames likes this.

  3. #3
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Many thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lvalue required as left operand of assignment
    By Thai guy in forum C Programming
    Replies: 4
    Last Post: 06-03-2012, 08:58 PM
  2. lvalue required as left operand of assignment
    By ModeSix in forum C Programming
    Replies: 6
    Last Post: 04-14-2011, 01:45 PM
  3. error: lvalue required as left operand of assignment
    By owjian1987 in forum C Programming
    Replies: 5
    Last Post: 02-11-2011, 12:34 PM
  4. lvalue required as left operand of assignment
    By joeman in forum C++ Programming
    Replies: 11
    Last Post: 03-27-2010, 01:36 AM
  5. lvalue required as left operand of assignment (??)
    By oospill in forum C Programming
    Replies: 3
    Last Post: 11-03-2009, 11:02 PM

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