array query

In the following code why does the third cout statement throw an error ?

1
2
3
4
5
6
7
8
9
10
  int main(int argc, const char * argv[])
{

    int c[] =  {1,2,3,4,5};
    std::cout<<c;
    std::cout<<*c;
    std::cout<<*c++;
    
    
}


Thanks !
*c++ is interpreted as *(c++). You can't use the increment operator on an array like that.
but you can do:
 
std::cout << *(c + 1);
why don't binary operators work like unary operands in these cases..?
Because it's trying to increment c, which is an array. I think you meant (*c)++, which will increment c[0]

Array names are constant pointers ,
so simple c++ is illegal in the above code ;
but if *(c+1) is legal why not *(c++) or *(++c) as c+1 == ++c
please clear my doubt and tell me how i am wrong ?
Last edited on
c+1 is not the same as c++ because c++ modifies c and c+1 does not. The key here is that you can't change c.
ohh yes c++ is equivalent to c=c+1 or c += 1
but in *(c+1) i am not modifying c…and c remains constant
Thanks!!
Last edited on
Topic archived. No new replies allowed.