Question

Explain addresses in C++ and pointers. What is the difference between the pointer (*) symbol, the...

Explain addresses in C++ and pointers.

What is the difference between the pointer (*) symbol, the deference operator (&) ?

Include 30 words

0 0
Add a comment Improve this question Transcribed image text
Answer #1

An address of a variable defines the location where the variable is stored.

For example :

int a =20;

This means a is a variable which is stored in memory with a value of 20.

To get the address of the variable, we use & operator.

Consider the following segment :

#include <iostream>

using namespace std;

int main() {

int a = 20;

cout << &a;

}

The value which is printed is the address of the variable a. This address which is displayed in the console is a logical address and not physical address. Thus, the logical address is transformed into physical address by operating system using memory management unit ( M.M.U.) and then this physical address in the memory stores the variable a with value 20.

In the above figure, the logical address 0x7ffde192689c is mapped to physical address 1012 of the main memory using MMU.

Difference between (*) symbol and ( & ) operator :

The (*) is called the dereference operator. This is used to return value stored in a particular memory location. In other words, it returns the value that the pointer pointing to a variable is holding.

For example :

int a = 20;

int *p;

p = &a;

cout << *p

This (*) operator will return the value of the variable whose address is stored in pointer p. Thus, *p will give the output 20.

(&) operator is called an address of operator as it stores the address of a variable with a particular value.

In the following segment :

int a = 20;

cout << &a;

&a will give the address of the variable a and it actually tells where in memory the variable a is located with value 20.

Add a comment
Know the answer?
Add Answer to:
Explain addresses in C++ and pointers. What is the difference between the pointer (*) symbol, the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT