Pointer movement problem in double


Dear Fellows,

Double type variables take 8 bytes storage.In the following example when pointer is incremented first time it moves 8 bytes forward but it is incremented 2nd time it moves 4 bytes forward...The question is that why 2nd time it moves 4 bytes instead of 8 bytes forward.

#include<iostream.h>
main()
{
double y[10];
double *yptr;
yptr = y;

cout<<"The meomory address of yptr="<<yptr<<endl;
yptr++;

cout<<"The memory address after the increment yptr="<<yptr<<endl;
yptr++;
cout<<"The memory address after the 2nd time increment yptr="<<yptr<<endl;

}

Last edited on
The question is that why 2nd time it moves 4 bytes instead of 8 bytes forward.


It doesn't. It moves 8 bytes. You must be interpretting the output incorrectly.
I'm not interpreting its output incorrectly but I think there's some problem with the compiler(DevC++).Which complier did you use to see its result.
I doubt any compiler would compile that incorrectly. That would be a pretty huge/disasterous bug that would have been quickly exposed and fixed.

What is the actual output you are getting?


I used MSVC++ 2010 express.

And while we're on the subject: DevC++ is ancient and decrepit. You shouldn't be using it. You're also using pre-standard code that shouldn't compile in modern compilers. You should throw away whatever book/source you're learning from and pick up something more recent.
Tyx for your useful advices.I'll also try MSVC++.

I'm getting the following answer:

The meomory address of yptr=0x22ff20

The memory address after the increment yptr=0x22ff28

The memory address after the 2nd time increment yptr=0x22ff30
Last edited on
0x22ff30 - 0x22ff28 == 8

Where's the problem?
how it's = 8??
is it not 2??

Pl define to some extent.
It's hexadecimal.

10 in hex is really sixteen
A is ten
B is eleven
...
F is fifteen
Topic archived. No new replies allowed.