1

This is basically just to help me understand pointers better, so if you guys can confirm/deny/explain anything it looks like I don't understand properly I would be most appreciative. The examples of using mailboxes, and aunts, and streets, and all that crap is just confusing.

int a = 5;
int b = &a; // b will be the memory address of 'a'
int *c = a; // c will be the value of 'a' which is 5
int *d = &a; // d will be a pointer to the memory address of 'a'
int &e = a; // what would this be?
void FunctionA()
{
 int a = 20;
 FunctionB(&a);
 // a is now 15?
}
void FunctionB(int *a)
{
 a = 15;
}

Thank you guys for any help, I am just trying to improve my understanding beyond all of the crappy metaphor explanations im reading.

sje397
41.9k9 gold badges90 silver badges106 bronze badges
asked Nov 16, 2010 at 2:37
3
  • Good answers here: programmers.stackexchange.com/questions/17898/… Commented Nov 16, 2010 at 2:40
  • Have you tried to compile all of these statements? Commented Nov 16, 2010 at 2:40
  • Think of e as an alias for a. Any time e is read, the value of a is returned. Any time you write to e, a is updated. Commented Nov 16, 2010 at 2:55

6 Answers 6

3

I'll take things one by one:

int b = &a; // b will be the memory address of 'a'

No. The compiler (probably) won't allow this. You've defined b to be an int, but &a is the address of an int, so the initialization won't work.

int *c = a;

No -- same problem, but in reverse. You've defined c to be a pointer to an int, but you're trying to initialize it with the value of an int.

int *d = &a;

Yes -- you've defined d to be a pointer to an int, and you're assigning the address of an int to it -- that's fine. The address of an int (or an array of ints) is what a pointer to int holds.

int &e = a;

This defines e to be a reference to an int and initializes it as a reference to a. It's perfectly legitimate, but probably not very useful. Just for Reference, the most common use of a reference is as a function parameter (though there are other purposes, of course).

void FunctionA() { int a = 20; FunctionB(&a); }
void FunctionB(int *a) { a = 15; }

To make this work, you need to change the assignment in FunctionB:

void FunctionB(int *a) { *a = 15; }

As it was, you were trying to assign an int to a pointer, which won't work. You need to assign the int to the int that the pointer points at to change the value in the calling function.

answered Nov 16, 2010 at 2:47
Sign up to request clarification or add additional context in comments.

1 Comment

I see some confusion between assignment and initialization in this response. Still +1'ed
1
int &e = a; // what would this be?

e is a reference to a. In this case the ampersand is not the 'address of' operator. You treat e as you would a normal (not a pointer) variable, but the value of e and of a will be the same no matter what you do to either (as long as both remain in scope) as essentially e is just an alias.

answered Nov 16, 2010 at 2:41

Comments

1

int a = 5;

So far so good.

int b = &a; // b will be the memory address of 'a'

That's actually a compilation error. You probably mean int *b=&a;. b is a POINTER to an integer. edit: If you mean to get the address in numerical form, you need to force the cast to an integer: int b=(int)&a;

int *c = a; // c will be the value of 'a' which is 5

This one is more confusing. At its core, a pointer is just a number, sure, but this kind of assignement is inherently not safe (as you can see, you're assigning 5 to a pointer, and trying to dereference that will most likely crash your program). If you really do want c to point at the memory location 5, you have to explicitly tell the compiler you know what you're doing: int *c=(int *)a.

int *d = &a; // d will be a pointer to the memory address of 'a'

This one is right, same as what you probably mean by the second one.

int &e = a; // what would this be?

e is a "reference" to a. Basically internally it's just a pointer to a, but you don't have to manually dereference it, the compiler handles it for you.

void FunctionA() { int a = 20; FunctionB(&a); // a is now 15? }

Yes.

void FunctionB(int *a) { a = 15; }

...assuming you write this as *a=15;. You're overwriting the VALUE pointed to by a, not the pointer itself.

You seem pretty confused by this overall, I recommend reading the book "Thinking in C++", it's really well written!

answered Nov 16, 2010 at 2:44

5 Comments

Try it yourself: ideone.com/L6KjU. Basically the compiler won't convert a pointer to a non-pointer (or vice-versa) without you explicitly telling it to. That's one of the main differences between C and C++, classes aside.
@akonsu: a in an int. &a is the address of an int. b is an int. b cannot be initialized to an address (without a cast anyways); it's an int. To hold an address of an int, you must use an "int pointer", with syntax "int *". So "int *b = &a" is fine, since here b is a pointer to the address of an int, not just an int.
thanks. i was just saying that int has enough room to hold an address value. i did not know that gcc at least is smart enough to catch these errors.
@akonsu, that's not true either, there are platforms where pointers are bigger than ints.
can this statement be parsed? can a syntax tree be constructed from it? yes. that is why i asked. on some platforms int is shorter than long as well, but assigning long to int is not a compilation error.
1

You've got plenty of good answers here for your specific example, so I'd like to share a general technique that I used to learn how pointers work when I was starting out.

Get a big sheet of graph paper and lay it lengthwise on the table in front of you. This is your computer's memory. Each box represents one byte. Pick a row, and place the number '100' below the box at far left. This is "the lowest address" of memory. (I chose 100 as an arbitrary number that isn't 0, you can choose another.) Number the boxes in ascending order from left to right.

+---+---+---+---+---+--
| | | | | | ...
+---+---+---+---+---+--
100 101 102 103 104 ...

Now, just for the moment, pretend an int is one byte in size. You are an eight-bit computer. Write your int a into one of the boxes. The number below the box is its address. Now choose another box to contain int *b = &a. int *b is also a variable stored somewhere in memory, and it is a pointer that contains &a, which is pronounced "a's address".

int a = 5;
int *b = &a;
 a b 
+---+---+---+---+---+--
| 5 | |100| | | ...
+---+---+---+---+---+--
 100 101 102 103 104 ...

Now you can use this model to visually work through any other combinations of values and pointers that you see. It is a simplification (because as language pedants will say, a pointer isn't necessarily an address, and memory isn't necessarily sequential, and there's stack and heap and registers and so on), but it's a pretty good analogy for 99% of computers and microcontrollers.

You can extend the model for real four-byte ints too...

int a = 5;
char b = 2;
 a a a a b
+---+---+---+---+---+--
| 0 | 0 | 0 | 5 | 2 | ...
+---+---+---+---+---+--
 100 101 102 103 104 ...
answered Nov 16, 2010 at 3:04

Comments

0

int &e=a; is a reference to "a".

and these are bugs:


int b = &a;
int *c = a;
answered Nov 16, 2010 at 2:43

Comments

0

I think you mean:

int *b = &a;, which makes a pointer called b that points to the value of a (b is a pointer which is the address of a)

int c = *b;, (or just int c = a if you only want c to have a's value) In this case, * dereferences the pointer b

FunctionA() See below, then a will be 15 (you're passing the address of a to FunctionB)

void FunctionB(int *a) {*a = 15;} sets the value to 15 (* dereferences)

answered Nov 16, 2010 at 2:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.