# Question on pointer usage

• 01-18-2003
Panopticon
Question on pointer usage
I know that when a pointer is incremented, it will point to the next element of its type. e.g:
Code:

```int a[5]={1,2,3,4,5},*p; p=a; cout<<*(p+4)<<endl; //will output 5```
This is because the addresses of elements of the array are all adjacent.
My question is applying the same principle, why doesn't the following work:
Code:

```int a,b,c,d,e,f,g,h,i,*p; p=&a; for(i=0;i<8;i++) *(p+i)=i*i; /*The loop should put into int a to h the squares of i as i increments*/```
Because the variables are all created at the same line, with nothing in between, does it mean their addresses are also adjacent?
• 01-18-2003
JoeSixpack
>does it mean their addresses are also adjacent?<

Not necessarily. The only way you can guarantee the kind of behaviour you expect is by using an array (or importing an asm module). Try checking the addresses of the variables in each situation.
• 01-18-2003
Polymorphic OOP
Quote:

Originally posted by JoeSixpack
>does it mean their addresses are also adjacent?<

Not necessarily. The only way you can guarantee the kind of behaviour you expect is by using an array (or importing an asm module). Try checking the addresses of the variables in each situation.

Actually, using commas in a declaration does guarantee that they are adjacent (least that's what chubsy taught us).

However, I'm not quite sure about the order. I checked msvc++ and the order is actually opposite (so if you start with p = &h it will work). Not sure if the order is standard though.
• 01-18-2003
JoeSixpack
>However, I'm not quite sure about the order. I check msvc++ and the order is actually opposite (so if you start with p = &h it will work)<

Maybe with commas; but try and get compilers to line up global/static objects up the way you'd think (****ing pain in arse). I'd imagine the backwardness was due to the stack; in which case it would be correct and all would be well.
• 01-18-2003
Polymorphic OOP
EDIT: Ahh, i see why. I didn't realize the stack expanded BACKWARDS in memory rather than forwards. Seems kind of odd. Anyone know of the reason for this?
• 01-18-2003
Sebastiani
*hint* : "the stack grows downward." ;)
• 01-18-2003
Panopticon
Thanx. it works as expected now.
• 01-19-2003
Polymorphic OOP
Quote:

Originally posted by Salem
Knowing the address of a, tells you NOTHING about the address of b, so I do hope you're just doing this for fun.
I was taught that comma's in declarations guarantee that they are adjacent in memory (as opposed to separate lines of declarations). So b would either be sizeof(int) above or sizeof(int) below in memory. Is this correct, or improper teaching?
• 01-19-2003
bennyandthejets
In my experience, declaring variables in any way will put them in the expected order. Ie:

Code:

```int a,b,c,d,e,f,g; int h; int i; int j; int k;```
They will be in order in memory. I've used Windows 98, Windows XP, Turbo C 2.01, and Microsoft Visual C++, and I've never seen any different behavior.