开发者

C++ Pointers and References Clarification

开发者 https://www.devze.com 2023-01-25 11:46 出处:网络
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

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 understan开发者_如何学Goding beyond all of the crappy metaphor explanations im reading.


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.


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.


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!


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  ...


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

and these are bugs:


int b = &a;
int *c = a;


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号