Question

In: Computer Science

How do you dereference a pointer? Explain the difference between a dereferenced pointer and the pointer...

How do you dereference a pointer? Explain the difference between a dereferenced pointer and the pointer itself.

Solutions

Expert Solution

What is a Pointer?

A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation.

Declaring pointers:

Pointer declarations use the * operator. They follow this format:

typeName * variableName;

int n; // declaration of a variable n

int * p; // declaration of a pointer, called p

In the example above, p is a pointer, and its type will be specifically be referred to as "pointer to int", because it stores the address of an integer variable. We also can say its type is: int*

The type is important. While pointers are all the same size, as they just store a memory address, we have to know what kind of thing they are pointing TO.

double * dptr; // a pointer to a double

char * c1; // a pointer to a character

float * fptr; // a pointer to a float

Pointer dereferencing:

Once a pointer is declared, you can refer to the thing it points to, known as the target of the pointer, by "dereferencing the pointer". To do this, use the unary * operator:

int * ptr; // ptr is now a pointer-to-int

  

// Notation:

// ptr refers to the pointer itself

// *ptr the dereferenced pointer -- refers now to the TARGET

Suppose that ptr is the above pointer. Suppose it stores the address 1234. Also suppose that the integer stored at address 1234 has the value 99.

cout << "The pointer is: " << ptr; // prints the pointer

cout << "The target is: " << *ptr; // prints the target

// Output:

// The pointer is: 1234 // exact printout here may vary

// The target is: 99

Difference between a dereferenced pointer and the pointer itself:

- Pointers can iterate over an array, you can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element. This is no matter what size the object is that the pointer points to.

- A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access it's members whereas a reference uses a ..

- A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address as the item it references.

- References cannot be stuffed into an array, whereas pointers can be.


Related Solutions

How do you explain the difference between the hyper globalist and the sceptical views of the...
How do you explain the difference between the hyper globalist and the sceptical views of the process of economic globalisation?
Do you understand the difference between absolute and relative advantage and how it relates to the...
Do you understand the difference between absolute and relative advantage and how it relates to the PPF Curve (IE where each are determined?)
How do you see the difference between quantitative vs. qualitative research? Do you think 1 is...
How do you see the difference between quantitative vs. qualitative research? Do you think 1 is more reliable than the other? Why? If you were conducting some marketing research on launching a new product or service for your employer/organization, which 1-2 specific research tools might you recommend to your supervisor to pay for?
Please explain the difference between a deduction and a credit.Which are the refundable credits?Why do you...
Please explain the difference between a deduction and a credit.Which are the refundable credits?Why do you think we have refundable credits?
How would you explain the difference between the SEC, the IRS, and the FASB to someone...
How would you explain the difference between the SEC, the IRS, and the FASB to someone who has never studied accounting or the financial markets?
a. What is the difference between coupon rate and yield to maturity? How do you use...
a. What is the difference between coupon rate and yield to maturity? How do you use the coupon rate to calculate the periodic payment received from a bond? b. What is the price of a bond that is currently trading at a yield of 10% and has a face value of $1,000? This bond still has exactly 5 years to maturity. This bond pays semi-annual coupon at an annual rate of 8% (i.e., each coupon is 4%). Show how you...
How do you identify the difference between a cost center, a profit center, and an investment...
How do you identify the difference between a cost center, a profit center, and an investment center? How do you prepare a segment margin income statement and evaluate a segment’s financial performance?
How do you understand the difference between a fiduciary model and a transactional, consumerist model of...
How do you understand the difference between a fiduciary model and a transactional, consumerist model of business–customer relationship? What reasons exist for the fiduciary model? Is there an ethical difference between the two?
Explain the difference between active and latent error. Give an example of each. Which do you...
Explain the difference between active and latent error. Give an example of each. Which do you think is the most common in health care? Why? This is for a discussion post. Please provide at a minimum 200 words in your response. If you can't provide 200 words, please don't attempt to provide a response. Thank you very much in advance.
A. How do you calculate net profit starting with sales? What is the difference between two...
A. How do you calculate net profit starting with sales? What is the difference between two ventures if one has very low net profit margin and the other with high profit margin? Is the difference attributed to management of costs and expenses? If yes, why? B. How is ROA related to Net Profit Margin and asset turnover?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT