declaring a variable

Oct 29, 2013 at 10:25am
When declaring a variable, are they stored next to each other? So for example would this show values of b and d?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
using namespace std;

int main()
{
int a=7;
int b=6;
int c=5;
int d=4;
int f=3;

int* p = &c;
cout << *(p+1) << endl;
cout << *(p-1) << endl;

}


Or is it necessary to declare pointers of other variables in order to.. make stuff work? If so, why? And why in that case *(p-1) would point to 0?
Last edited on Oct 29, 2013 at 10:25am
Oct 29, 2013 at 10:36am
No, AFAIK, only array elements can be assumed to be stored sequentially in memory.

The method you described may or may not work in a system, but it is definitely not portable code.
Topic archived. No new replies allowed.