Thread: lvalue required as increment operand

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    lvalue required as increment operand

    i keep experiencing this particular error while doing pointer arithmetics in C/

    e.g

    Code:
    #include <stdio.h>
    
    int strcmp(char *s, char *t);
    
    int main(){
    
        char s[] = "hello";
    
        printf("%c", *++s);
    
    }
    the error is

    C:\Users\xx\Documents\strcmp.c|9|error: lvalue required as increment operand|
    but when i changed *++s to *(s+1); it runs and outputs the character 'e'.

    are they not essentially the same? as far as i know this works when used for parsing commandline arguments using switch but for an unknown reason barely works normally

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thagulf2017
    but when i changed *++s to *(s+1); it runs and outputs the character 'e'.

    are they not essentially the same?
    No, they are not. *++s pre-increments s, dereferencing the result of the pre-increment. *(s + 1) adds 1 to s, dereferencing the result of the addition. This is the big difference between saying s = s + 1 and saying s + 1. Put it another way: ++s changes s, s + 1 does not. Hence, in your original example, s + 1 is permitted, but ++s is not because s is an array, and arrays cannot be modified (although the content of an array of non-const elements can be modified).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lvalue required as increment operand
    By justine in forum C Programming
    Replies: 13
    Last Post: 11-30-2012, 01:26 AM
  2. lvalue required as left operand of assignment
    By thames in forum C Programming
    Replies: 2
    Last Post: 10-30-2012, 09:43 AM
  3. fixing lvalue quired as increment operand
    By candy.chiu.ad in forum C Programming
    Replies: 9
    Last Post: 03-07-2011, 02:56 PM
  4. Lvalue required as increment operand
    By .C-Man. in forum C Programming
    Replies: 4
    Last Post: 10-13-2010, 02:41 PM
  5. lvalue required as increment operand compile error
    By canadatom in forum C Programming
    Replies: 8
    Last Post: 06-13-2009, 11:49 AM

Tags for this Thread