0

I am practicing for a c++ midterm, and I can't see why the following code is incorrect.

int ip[] = {6, 7, 2, 4, -5};
for (int i = 0; i < 5; ++i, ++ip)
    cout << *ip;

My suspicion is that is it something to do with the -5, but I'm lost, and I'd really like to get this resolved.

2
  • an array variable is equivalent to a constant pointer. So it can't be incremented or modified in any way. Commented Mar 5, 2012 at 9:54
  • @Pramod No, an array variable is not equivalent to a constant pointer. For example, sizeof(array) is generally not the same as sizeof(pointer). Commented Mar 5, 2012 at 11:42

8 Answers 8

3

You cannot increase ip, since it is an array, and not a pointer - so its value [ip] is fixed.

So, the problem is with the expression ++ip

Sign up to request clarification or add additional context in comments.

6 Comments

They are not, arrays have special semantics. You can also check what is the value of &ip [the answer is &ip == ip], which is not true for pointers.
@Zack: That's a common misconception. It would be better to say that a pointer value can be produced from any array and that pointers can be used to simulate arrays using pointer arithmetic (so they certainly look very similar). But for the language itself, pointers and arrays very much distinct entities.
While I am at it, int* ip = (int*)20; ++ip; this code would end with 24 for ip, correct? Because integer pointer increment by 4 bytes each time?
You're probably also confused by the fact that arrays decay into pointers in C and C++, but they are not precisely the same thing. If you take that for loop that is currently giving you a syntax error and move it inside a function that looks like void print_array(int ip[], int n), it will actually work fine because passing the array into a function cause the array to decay to a pointer that can be incremented.
@Zack Yes and no. ip can never have the value 24, because 24 is an integer value, and pointers aren't integers. But incrementing an int* does cause it to point to the next element of the int[], providing the int* points into an int[]. (Whether int is four bytes is another question: I'm aware of systems where it is six bytes, and I've worked a lot on systems where it was only 2 bytes.)
|
1

The error is that you can't increment the value of a static pointer, aka an array.

The easy solution is to simply use the indexing operator [].

int ip[] = {6, 7, 2, 4, -5};
for (int i = 0; i < 5; i++)
    cout << ip[i];

Comments

1

You can access these elements directly by using index:

int ip[] = {6, 7, 2, 4, -5};
for (int i = 0; i < 5; ++i)
    cout << ip[i];

or if you want to use pointer arithmetic for this purpose, you could use temporary variable - pointer that will point to first element of this array:

int ip[] = {6, 7, 2, 4, -5};
int* myPtr = ip;
for (int i = 0; i < 5; ++i, ++myPtr)
    cout << *myPtr;

Note that int* myPtr = ip; is equal to int* myPtr = &ip[0].

Comments

1

The reason for this is:

  1. when it comes to an array ip[],ip is pointing to the first element of array and it is fixed and it cannot be changed(it is like a constant pointer i.e pointing to a fixed memory location.)

    2.so when you are incrementing it you are violating the condition that's why you are getting an error

try this instead

int ip[] = {6, 7, 2, 4, -5};
for (int i = 0; i < 5; ++i)
    cout << ip[i];

Comments

0

ip is a static array.. you cannot increase it! eventually you can create another pointer that points to ip:

int* p = ip;

then increment p.

Comments

0

you need to increase each value of the array ip[] separately

Comments

0

Pointers and arrays are distinct things. Pointers decay to arrays, but it doesn't work the other way around. The operator++ is not defined on arrays, so ip decays to a pointer, the pointer (unnamed temporary) is incremented, but you can't store it back into ip, because ip is an array.

The code below should work, as you expect.

#include <iostream>

int main(){
        int ip[] = {6, 7, 2, 4, -5};
        int *p=ip;
        for (int i = 0; i < 5; ++i, ++p)
            std::cout << *p;
}

Comments

0

Just to formalize somewhat what others are saying: ip is an array, not a pointer. It converts implicitly to a pointer, but the results of an implicit conversion are an rvalue, and the ++ operator requires an lvalue. Other than the implicit conversion (which doesn't occur in all contexts), there is no real relationship between arrays and pointers.

Note too that in C++, [] is not defined for array types, only for pointers (or for types with user defined overloads). It only works with arrays because of the implicit conversion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.