I'd like to make one variable always equal to 1/3rd of another variable. I've tried the following...

Code:

int x;
int *p;

p = &x/3;

But I get "error C2296: '/' : illegal, left operand has type 'int *'"
How do I get this to work? Thanks in advance

02-12-2006

Richie T

Thats not the way pointers work, they store memory addresses,
not actually the data. The code you posted attempts to divide
the physical memory address of the variable x by 3, and assign
it to the pointer p. Obviously that won't do so here's what you
want:

Code:

int x = 3;
int *p;

p = &x;

cout <<"x/3 is equal to: "<<*p/3;

if you dont understand that then suggest reading up a bit on
pointers, i found them tricky until i actually took a minute to
sit down with them and i found that they're not too bad at all.