Basic Pointer/Address Question

Hello all, I just have a quick question about pointers/adresses. If I am being asked to print the value of a variable and the address of that variable ,is the address what follows the apprehend sign?
1
2
3
4
5
6
7
//Variables
 int num1= 10;
int *pointy;
pointy= &num1;
cout<<"Value of pointy"<<pointy;
cout<<"Address of pointy"<<&num1;
//WOULD I BE PRINTING &num1 TO GET ADDRESS OR IS THIS WRONG? 
Hello clayoda,

Thiss may help to explain:
1
2
3
4
5
6
7
8
9
10
	//Variables
	int num1 = 10;
	int *pointy;

	pointy = &num1;  // <--- This sets "pointy" to the address of "num"

	std::cout << "  Value of pointy  " << *pointy << '\n';      // <--- Prints the value of "pointy". Dereference pointer to get to the value.
	std::cout << "Address of pointy  " << pointy << std::endl;  // <--- Prints the address of "pointy".
	std::cout << "  Address of num1  " << &num1 << std::endl;   // <--- Prints the address of "num1".
	std::cout << "    Value of num1  " << num1 << '\n';         // <--- Prints the value of "num1". 


Andy

Edit: typo
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
	// a variable
	int var { 27 };

	// a pointer to a variable
	int* ptr { &var };

	std::cout << "var: " << var << ",  ";      // variable's value
	std::cout << "addr var: " << &var << '\n'; // variable's address

	std::cout << "*ptr: " << *ptr << ",  "; // value pointed to
	std::cout << "ptr: " << ptr << ",  ";   // address of pointed value
	std::cout << "&ptr: " << &ptr << '\n';  // address of pointer
}
The dereference operator (*) is used with the pointer variable to get the value stored at that address. Your program should be like this:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
 
int main (){
int num1= 10;
int *pointy;
pointy= &num1;
cout<<"Value of the variable: "<<*pointy<<"\n";
cout<<"Address of the variable: "<<pointy;
}

C++ Pointers: https://www.alphacodingskills.com/cpp/cpp-pointers.php
@Handy Andy

Some of your comments seem to be a little bit of out of step. I would have thought that:
pointy = the value of pointy (itself), which is the address of num1 in this case
*pointy = the value of the variable pointed to by pointy, i.e. value of num1

For the address of pointy you'd need &pointy

Andy
Last edited on
@andywestken You're right. As enthusiastic as Handy Andy is, there's lots he's only half-understood, and he frequently gives misleading or downright incorrect advice.
@MikeyBoy,

If I have it wrong or misunderstand what I have read please correct me so I can learn. Do not just temm everyone that I am wrong.

Andy
Andywestken has already explained how you were wrong and corrected you. What possible purpose would it serve for me to repeat what he's already said?

And the last time I corrected you over some misinformation you gave, your response was some nonsense gobbledigook to try and twist words to make it sound like you were right all along, rather than giving any appearance of making a good-faith effort to admit your mistakes and learn. So you'll understand if I have grave doubts about your sincerity.
Last edited on
Topic archived. No new replies allowed.