Zack
Zack

Reputation: 13972

c++ pointer/array distinction

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.

Upvotes: 0

Views: 218

Answers (8)

ken
ken

Reputation: 836

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];

Upvotes: 1

James Kanze
James Kanze

Reputation: 153909

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.

Upvotes: 0

LihO
LihO

Reputation: 42083

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].

Upvotes: 1

tokage
tokage

Reputation: 186

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;
}

Upvotes: 0

Pben
Pben

Reputation: 1101

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

Upvotes: 0

andrea.marangoni
andrea.marangoni

Reputation: 1499

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.

Upvotes: 0

Johan Lindskogen
Johan Lindskogen

Reputation: 1306

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];

Upvotes: 1

amit
amit

Reputation: 178441

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

Upvotes: 3

Related Questions