-3

It is said that,in c,b++; is equal to b=b+1; if this is the fact test++ in my code why generate a compile time error. test+1 is working well but test++ is not working.but why?

#include<stdio.h>
int main(void)
{
   char test[80]="This is a test";
   int a=13;
   for(;a>=0;a--)
  {
        printf("%c",*(test++);
  }
}
Noman Hanafi
  • 377
  • 2
  • 8

3 Answers3

1

The ++ and -- operators are not defined for arrays.

v++; would be the same as v = v + 1;. Assumed v was typed an array this would imply assigning to an array, which is not defined.

alk
  • 69,737
  • 10
  • 105
  • 255
1
char test[80] = "This is a test";
char *p = test;

for(int a = 0; a < 14; a++)
{
    printf("%c", *(p++));
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • And how does this answer the question? – alk Apr 18 '16 at 18:24
  • @alk It provides a possible solution. Other answers cover what's wrong. – Mateen Ulhaq Apr 18 '16 at 18:27
  • The question is "*but why?*", not "but how?". I do not want to say additionally providing a solution is wrong, but without giving a reason why the initial approach doesn't work, there is missing an essential part. – alk Apr 18 '16 at 18:50
1

Well, for one thing, b++ is not the same as b=b+1.
But even if it were -- I think you'll find you get a similar error if you try test = test + 1.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103