answersLogoWhite

0

Dereferencing a pointer returns the value stored at the memory location being pointed at, according to the type of pointer.

Consider the following code. c is a char type and p is a pointer to a char type. c is assigned the value 'X' (88 decimal, 0x58 hexadecimal) while p is assigned the address of c, using the addressof operator &.

The remainder of the code prints the address and value of both c and p, and also the dereferenced value of p. Note that you cannot dereference c since it is an alias for a reference, not a pointer to a reference.

Note how the value of p is the memory address of c, while the dereferenced value of p is the value of p. note also that pointers are variables like any other, and therefore occupy memory of their own, as shown by the address of p.

#include

using namespace std;

int main()

{

char c = 'X';

char * p = &c;

printf( "Address and value of c:\n" );

printf( "&c = 0x%.8x\n", &c );

printf( " c = 0x%.2x (%u) '%c'\n", c, c, c );

cout << endl;

printf( "Address, value and dereferenced value of p:\n" );

printf( "&p = 0x%.8x\n", &p );

printf( " p = 0x%.8x\n", p );

printf( "*p = 0x%.2x (%u) '%c'\n", *p, *p, *p );

cout << endl;

return( 0 );

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What is the difference between c plus plus and java programming?

Java doesn't have pointers. C++ has pointers.


What is the difference between pointers in c and c plus plus?

Nothing.


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


What are the storage allocation in C plus plus?

They mostly deal with pointers and new operators in memory.


How do you write a reverse function in C plus plus without pointers?

You can either use references or you can simply return the result by value. Note that in C++, unlike C, references are not the same as pointers. C++ references are aliases, alternate names for existing objects, whereas pointers are just variables that may contain a memory address that can be dereferenced.


Dereferencing operator in c?

When we are talking about dereferencing in C we are talking about the pointers and how to get value from them, not address.Dereferencing operator notation is "*".Here is a simple example of dereferencing:int num;int pNum*;pNum = &num; /* make pNum pointer point to numlocation in memory/stack */*pNum = 7; /* setting pNum value to 7. Note! numvalue becomes 7 too, because pNum points to the same memory location as num */


Do you have pointer concept in c plus plus language?

Yes. All string variables are pointers as are other arrays.


Difference between array and pointers in c plus plus?

A pointer is simply a variable that can store a memory address and has the same purpose in both languages. The only real difference is that C++ pointers can point at objects (instances of a class) and indirectly invoke their methods, whereas pointers in C (which is not object oriented) cannot.


What does multiplying a pointer by 5 in C plus plus mean How does it change the pointer's value?

Multiplication is yet another thing, what you should never do with pointers.


What is a segmentation fault?

An error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers in the source code, dereferencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.


What is a segment fault?

An error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers in the source code, dereferencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.


What is meant by passing function to another function in c plus plus?

Just as pointers can point to variables, pointers can also point to functions. Thus you can pass function pointers to functions. In so doing, you can alter the behaviour of the function by having it call dynamically call arbitrary functions rather than just preset functions.