pointers address

Pages: 12
Hello, I have been learning about pointers, and now on pointers to pointers, and made a small program to test all the data stored. I wanted to check the pointers address and then checker the address that was stored at the pointer that was pointing to that pointer. But it doesnt seem to be returning correctly. I am unsure if i am doing so incorrectly, or if i have misunderstood the pointer to pointer and the address that it holds

I have given the program i created below, and for the lines that print out 'pointer' and '&pointerTo' i expected to print the same memory address but they do not? Should they not do this? I thought pointer would print its own memory address and then &pointerTo would print the memory address that it is holding which is the memory address of pointer? Am i incorrect in thinking this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  int var = 9;
	int* pointer;
	int** pointerTo;

	pointer = &var;

	cout << "Address of variable : " << &pointer << "\n\n";

	cout << "Value stored at address pointed to by pointer : " << *pointer << "\n\n";

	cout << "Address of pointer : " << pointer << "\n\n\n";

	pointerTo = &pointer;

	cout << "Address of pointer : " << pointer << "\n\n";

	cout << "Address of pointerTo : " << pointerTo << "\n\n";

	cout << "Address of pointer stored at pointerTo : " << &pointerTo << "\n\n";

	cout << "Value pointed to by pointer pointed to by pointerTo :/ : " << **pointerTo << "\n\n";
cout << "Address of variable : " << &pointer << "\n\n";
Well that's not going to be the address of the variable. &pointer is the address of the pointer. &anything is the address of the anything (well, more accurately, a pointer holding the address of the anything).

cout << "Address of pointer : " << pointer << "\n\n\n";
No, that's going to give you the address of the variable. You know it will; look, that's what you stored in it: pointer = &var;
Last edited on
ok so I have them mixed up? If i want to find the address of the variable i should just call pointer, and to get the address of the pointer it should be &pointer?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {
    using std::cout;
    int n = 42;
    int *p = &n;
    cout << "Address of  p: " << &p << '\n';
    cout << "Value   of  p: " <<  p << '\n';
    cout << "Address of  n: " << &n << '\n';
    cout << "Value   of  n: " <<  n << '\n';
    cout << "Value   of *p: " << *p << '\n';
}

Address of  p: 0x7ffc6a617f60
Value   of  p: 0x7ffc6a617f6c
Address of  n: 0x7ffc6a617f6c
Value   of  n: 42
Value   of *p: 42
I think this is a pretty good tutorial on pointers; what they are and why you need them. It goes over multiple pages.
https://computer.howstuffworks.com/c20.htm
I think terminology is important in understanding pointers.

A pointer is just an address. It is nothing special. It is just a 4 byte or 8 byte (depending on the architecture) integer that represents an address in memory. Lets look at an analogy.

An int variable holds an "integer." An integer looks like this: 6
A double variable holds a double-precision floating point number, which looks like this: 6.0
A char can be used to hold a single ascii character, which looks like this: $
A pointer can be used to hold an address, which looks like this: 0x65342334, or &some_variable.

The confusion lies in the different terminology used. You can technically use the terms "address" and "pointer" interchangeably. For example, (int*)0x45453423 is a pointer to some location in memory (on a 32-bit platform) that stores an int.

So if you have a pointer A, which stores the address of var (or a pointer to var) like so:

int* A = &var

The value of the pointer variable "A" will be set to equal the pointer (address) where var is stored in memory. Now, if you have a second pointer "B" that points to the address A is stored at,

int** B = &A

Then "B" will store the address where A is located at in memory. It has nothing to do with var at this point, since the memory location where A is does not depend on var
Last edited on
toasty wrote:
A pointer is just an address

No.
An address is an address.
A pointer is an object that holds an address.
1 to 4 summarizes how it works.
5 answers your question about getting the address of the pointer, and 6 de-references that. But I don't think they would be much use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main()
{
    // 1. VARIABLE
    int variable = 100;
    std::cout << variable << '\n';
    
    // 2. ADDRESS
    std::cout << &variable << '\n';
    
    // 3. POINTER
    int* pointer = &variable;
    std::cout << pointer << '\n';
    
    // 4. DE-REFERENCE POINTER
    std::cout << *pointer << '\n';
    
    // 5. ADDRESS OF POINTER
    std::cout << &pointer << '\n';
    
    // 6. DEREFERENCE THE ADDRESS OF POINTER
    std::cout << *(&pointer) << '\n';
    
    return 0;
}


dutch wrote:
A pointer is an object that holds an address.


This is just semantics.

When you look at this line:

 
auto var = 5;

You would say var is initialized with an int. 5 being an rvalue of type "int," or just "int" for short. "Integer" is also valid.

 
auto var = 5.0;

You would say var is initialized with a double. 5.0 being an rvalue of type "double," or just "double" for short. "Double-precision floating point number" is also valid.

1
2
int something;
auto var = &something;

You would say var is initialized with an int* (int pointer). &something being an rvalue of type int* (int pointer), or just "pointer" for short. "Address of something" is also valid.

To clarify, you can use the term "pointer" and "address" interchangeably in the semantic construct of C++ when referring to the rvalue. You could indeed say "address," but there is no "address" data type. There are only pointer data types. You could argue that "addresses" are merely rvalues of pointer data types, and thus should be called addresses. I could argue that "addresses" are merely rvalues of pointer data types, and thus should be called "pointer." At the end of the day, it doesn't matter what it is called. It devolves into a meaningless Swiftian debate.

Of course, main memory has addresses. You typically wouldn't call them "pointers." But in the semantic constructs of C++, it doesn't really matter. Just like the term "rvalue" makes sense within the semantic construct of C++, but not much elsewhere.

The reason I make this distinction is for OP to understand what a pointer really is in hardware (Hint: its not really a giant arrow that sticks out of the silicon and points to somewhere inside main memory).
Last edited on
This is just semantics.

You're the faggot that said terminology was important.
What a fucking liar!
You're the faggot that said terminology was important.

Important doesn't necessarily mean accurate. Terminology is important to understand it, but that doesn't mean he's going to speak like a text book. Saying a pointer IS an address is not completely accurate, but it gets the point across.
dutch wrote:
You're the faggot that said terminology was important. What a fucking liar!


Thank you for your input. To clarify, in my opinion,

 
&var


Can be referred to as "pointer-to-var" or "address-of-var." I have heard it referred to as both and there is typically no confusion when using either term. Given that it is technically an rvalue of some pointer type (like int*), it is not wrong to refer to &var as a pointer, just like you would refer to 5.0 as a double versus calling it a "double precision floating point literal." C++ specifically refers to double precision floating point values as "double," but other languages may use a different term for it. Terminology is indeed important, and thus I used the terminology "pointer," which is consistent with the terminology used for the rvalues of all other primitive data types like double and float.

So, to conclude, it is equally as valid for me to use the term "pointer" for &var, since it is just an rvalue of a pointer type.

Though, I understand that my comment seemed to have incited some anger and deep insecurities on your part, but I have noticed from your other comments in this forum that this is your natural response when embarrassed and proven wrong. You're only human, after all, so I don't blame you for displaying this primal aggression when faced with a conflict of opinion.

Thanks for playing, though, Dutch.
Last edited on
it is not wrong to refer to &var as a pointer

I'd say it's only similar to a pointer. A pointer itself is a container for a memory address. &var is a memory address that is going to be only temporarily saved in a register if not assigned to a pointer.

Since a pointer has it's own memory address where it stores the memory address that it points to, I'd say it's not very accurate to say that the pointer is itself a memory address.
I'd say it's only similar to a pointer. A pointer itself is a container for a memory address.


And an "int" is a container for an "integer." And a "double" is a container for a "double precision floating point value." Yet, when we see something like 5.0, we typically say "that's a double," and when we see 5.0f we say "that's a float." It's all a matter of perspective. Though I understand others may not hold the same opinion on this, it's just the terminology that I believe makes more sense when referring to rvalues of a specific data type like int*. My view is consistent with how we treat every other data type in C++. I believe OP's confusion stemmed from the different terminology used by programmers to refer to rvalues of pointer types (addresses), which is why I simply used the term "pointer"

If the term "address" instead of "pointer" in reference to the data types (such that int* would become int-address) had been adopted, then we wouldn't be having this conversation. But given that is not the case, I decided to treat rvalues "pointer" types just like any other standard data type: refer to the rvalue equivalents as "pointers"

I personally reserve the term "address" to when I am specifically speaking about hardware or virtual memory, or other lower-level details, and typically use the term "pointer" when speaking in a purely C++ semantic context.
Last edited on
when we see something like 5.0, we typically say "that's a double,"

I've actually always found that a bit strange. I've always separated types with values, opting instead to say, "it's a decimal number, so you should use a double to store it."

This is why I'd say that it's not accurate to say a pointer is a memory address, anymore than it is accurate to say 5.0 is a double. These phrases seem to just be a matter of expedience when getting a point across.


I don't know what's up with dutch; he's been acting this way for a while now. Not sure if his behavior is something new or if he's always been like this.
I've actually always found that a bit strange. I've always separated types with values, opting instead to say, "it's a decimal number, so you should use a double to store it."


Yeah, I definitely get your point. In my experience, the difference really doesn't matter and most people understand what you mean when you refer to rvalue literals as their underlying type. To me an rvalue is inherently linked to its underlying type, and thus 5.0 and 5.0f are two different types of "double" and "float" respectively. To me, calling both of them a "decimal number" seems ambiguous, since they are two different types, but to each his own, I guess. I just use the term that makes sense to me in that particular context.

I don't know what's up with dutch; he's been acting this way for a while now. Not sure if his behavior is something new or if he's always been like this.


Well afaik he's always had a short fuse every time I've seen someone disagree with him. Usually devolves from a rational, respectful conversation into explosive anger. It almost makes me want to purposely disagree with him over something trivial to watch him get all worked up and offended lol.
Last edited on
Frustration aside, dutch is right. A pointer is an object that may hold the address of another object. It is not an address.
Frustration aside, dutch is right. A pointer is an object that may hold the address of another object. It is not an address.


As I have said and substantiated before, you are correct. That can indeed be referred to as an "address". However, it is not wrong in calling something like &var a pointer itself. It technically is an rvalue of a pointer type. You can dereference it: *&var. It can be set to a variable: auto var = &intvar, in which case you would say that "var is being initialized with an int* (int pointer)," which is technically correct because &intvar is an rvalue of type int*, which is a pointer. My justification for this is the same reason we call rvalues of other primitive types like double and float as the name of the type itself.

5.0 is a double (rvalue of type double)
5.0f is a float (rvalue of type float)
&intvar is an int* (rvalue of type int*), or simply a pointer for short.

You could technically single out the last one and call it an "address," but that omits the type information associated with it. &intvar is inherently and will always be deduced as an int* (an int-pointer). The reason I called it a pointer is for OP to understand that, because it seemed that his confusion was the different terminology used for pointer rvalues (which are addresses).

I apologize for not subscribing to whatever religion it is that forces programmers to speak like a textbook on every issue.
Last edited on
I apologize for not subscribing to whatever religion it is that forces programmers to speak like a textbook on every issue

I feel that
It comes with time. Programming demands precision, and in a language such as C++ where direct memory access is a standard utility, sloppy language leads to misunderstandings and mistakes. Programmers eventually end up speaking precisely because they get sick of misunderstandings and mistakes. The compiler has no mercy.

Having a precise understanding yet being able to explain it simply to the less experienced is an art and a craft.

This, for example:
You can technically use the terms "address" and "pointer" interchangeably
Except that a pointer is an object in memory, typically 4 or 8 bytes in the typical system people around here run into, that will be interpreted in particular ways and can be applied in particular ways, and an "address" is an idea, a concept, a way of specifying a particular memory location. There are circumstances where they're interchangable words; there are circumstances where they're not.
Last edited on
Pages: 12