0

OK, first let me provide 2 programs:

Program 1:

#include <iostream>

using namespace std;

int main()
{
    int a[5], i;
    int *ptr;

    ptr = a;

    cout << "Enter the elements of the array:" << endl;
    for (i = 0; i < 5; i++)
    {
        cin >> a[i];
    }

    cout << endl;

    cout << "*ptr:";
    for ( i=0 ; i<5 ; i++ )
    {
        cout << *ptr ;
        *ptr++ ;
    }

    cout << "&a[i]:" << endl ;
    for (i = 0; i < 5; i++)
    {
        cout << &a[i] << endl;
    }

    cout << endl ;

    cout << "ptr:" << endl;
    for (i = 0; i < 5; i++)
    {
        cout << (ptr+i) << endl;
    }

    return 0;
}

Output:

Enter the elements of the array: 1 2 3 4 5
*ptr: 12345

&a[i]:
0018FF30
0018FF34
0018FF38
0018FF3C
0018FF40

ptr:
0018FF44
0018FF48
0018FF4C
0018FF50
0018FF54

Program 2:

#include <iostream>

using namespace std;

int main()
{
    int a[5], i;
    int *ptr;

    ptr = a;

    cout << "Enter the elements of the array:" << endl;
    for (i = 0; i < 5; i++)
    {
        cin >> a[i];
    }

    cout << endl;

    cout << "*ptr:" << endl ;
    for ( i=0 ; i<5 ; i++ )
    {
        cout << *(ptr+i) ;
    }

    cout << "&a[i]:" << endl ;
    for (i = 0; i < 5; i++)
    {
        cout << &a[i] << endl;
    }

    cout << endl ;

    cout << "ptr:" << endl;
    for (i = 0; i < 5; i++)
    {
        cout << (ptr+i) << endl;
    }

    return 0;
}

Output:

Enter the elements of the array: 1 2 3 4 5

*ptr: 12345

&a[i]:
0018FF30
0018FF34
0018FF38
0018FF3C
0018FF40

ptr:
0018FF30
0018FF34
0018FF38
0018FF3C
0018FF40

From the above programs, we can see that *ptr in both the cases displays the same output.

I know that the code in Program 2 is the correct way of incrementing when dealing with pointers.

BUT, the ptr in both the programs are not same. I am pretty sure that the for loop which is used to display *ptr in Program 1 is responsible for this mess. I want to know what is happening in the for loop to display *ptr in Program 1 because of which ptr is affected.

Parthib Biswas
  • 471
  • 1
  • 7
  • 17

5 Answers5

2

In first program ptr is modifying itself.
Note that *ptr++; is equivalent to *(ptr++); which altimately equivalent to

*ptr;
ptr++;  

Adding i to ptr in the last for loop makes ptr to point somewhere else, unallocated memory location, instead of elements of a. That's why you are getting different address than that of address of array elements in your first program.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

In the first program, you modified ptr, then in the third loop added values to its address, without resetting to the beginning of the array.

The second loop modifies the value of ptr:

for ( i=0 ; i<5 ; i++ )
{
    cout << *ptr ;
    *ptr++ ;
}

Then in the fourth loop you add values to its address without resetting it back to the beginning of the array.

cout << "ptr:" << endl;
for (i = 0; i < 5; i++)
{
    cout << (ptr+i) << endl;
}

With each iteration, you add only one, but the value comes out four larger each time, because a unit increment is the size of an int.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • OK. This question might be little stupid but is there any 1 byte version of `int`? – Parthib Biswas Jul 13 '14 at 18:33
  • There's a minimum size of two bytes for an int, according to this answer: http://stackoverflow.com/questions/589575/size-of-int-long-etc . You can, however, represent an integral value with a 1-byte char. – Andy Thomas Jul 13 '14 at 18:45
1

After this loop

   for ( i=0 ; i<5 ; i++ )
    {
        cout << *ptr ;
        *ptr++ ;
    }

in the first program ptr points beyond the last element of the array because inside the loop ptr was increased. That to get the same result as in the second program you have to reset the pointer to point to the first element of the array after this loop

   for ( i=0 ; i<5 ; i++ )
    {
        cout << *ptr ;
        *ptr++ ;
    }

   ptr = a;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

In the first program you are updating the pointer (changing the address the pointer is holding). Hence , after the loop, pointer is pointing beyond the last address of the array

        *ptr++ ;

In the second program you are not updating the address contained in the pointer.

*(ptr+i)
Rukshan Perera
  • 150
  • 2
  • 9
0

Ok, just to clarify one thing.

There is nothing wrong with incrementing a pointer as supposed to adding to it. Incrementing pointers is the same concept as that behind iterators and is widely used in existing code.

Stian Svedenborg
  • 1,797
  • 11
  • 27