C++ Pointers and References Clarification -
this me understand pointers better, if guys can confirm/deny/explain looks don't understand appreciative. examples of using mailboxes, , aunts, , streets, , crap confusing.
int = 5; int b = &a; // b memory address of 'a' int *c = a; // c value of 'a' 5 int *d = &a; // d pointer memory address of 'a' int &e = a; // be? void functiona() { int = 20; functionb(&a); // 15? } void functionb(int *a) { = 15; }
thank guys help, trying improve understanding beyond of crappy metaphor explanations im reading.
i'll take things 1 one:
int b = &a; // b memory address of 'a'
no. compiler (probably) won't allow this. you've defined b
int
, &a
address of int
, initialization won't work.
int *c = a;
no -- same problem, in reverse. you've defined c
pointer int
, you're trying initialize value of int.
int *d = &a;
yes -- you've defined d
pointer int, , you're assigning address of int
-- that's fine. address of int (or array of int
s) pointer int
holds.
int &e = a;
this defines e
reference int
, initializes reference a
. it's legitimate, not useful. reference, common use of reference function parameter (though there other purposes, of course).
void functiona() { int = 20; functionb(&a); } void functionb(int *a) { = 15; }
to make work, need change assignment in functionb:
void functionb(int *a) { *a = 15; }
as was, trying assign int
pointer, won't work. need assign int
int
pointer points at change value in calling function.
Comments
Post a Comment