0

In the first program, the output is C++ ++ and i understood that s++ increments the address and the printf statement prints the string from the new base address.

#include<stdio.h>

void main()
{
   char *s = "C++";

   printf("%s ", s);
   s++;
   printf("%s", s);
}

now i have replaced the character pointer to a character array and tried to print the string before incrementing and after incrementing. I do not understand why my second program results in following compiler error "error: lvalue required as increment operand s++;". Thanks in advance.

#include<stdio.h>

void main()
{
   char s[] = "C++";

   printf("%s ",s);
   s++;
   printf("%s",s);
}
krish
  • 1
  • 1
  • This is one of the differences between a pointer and an array. Pointer arithmetic should not be misunderstood as *"array arithmetic"*. – Ardent Coder May 22 '20 at 14:42
  • 2
    Does this answer your question? ["lvalue required" error when trying to increment array](https://stackoverflow.com/questions/13667191/lvalue-required-error-when-trying-to-increment-array) – Saucy Goat May 22 '20 at 14:42

0 Answers0