I think that programming languages newer than C just adopted it's syntax. Everyone who konows C can understand it Answer from Whole-Dot2435 on reddit.com
🌐
W3Schools
w3schools.com › cpp › cpp_pointers_dereference.asp
C++ Dereferencing
In the example from the previous page, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). However, you can also use the pointer to get the value of the variable, by using the * operator (the dereference operator):
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › dereference-pointer-in-c
Dereference Pointer in C - GeeksforGeeks
Dereferencing is, use of a pointer to access the value whose address is being stored. We use the * operator to get the value of the variable from its address.
Published   2 weeks ago
Discussions

Why is the dereference operator generally a prefix in most langauges?
I think that programming languages newer than C just adopted it's syntax. Everyone who konows C can understand it More on reddit.com
🌐 r/Compilers
18
4
March 22, 2024
c++ - Pointer dereference operator ( (*) vs -> ) - Stack Overflow
Is there a general difference between doing (*ptr).method() vs ptr->method() I saw this question in a comment on another question and thought I would ask it here. Although I just remembered t... More on stackoverflow.com
🌐 stackoverflow.com
When to use dereference operator for pointers C++ - Stack Overflow
I'm currently studying C++. Not in school. Using books, tutorials and practice. One thing that confuses me and I haven't been able to track down an answer is when to use the dereference operator (... More on stackoverflow.com
🌐 stackoverflow.com
Pointer dereference operator
Sign up · Log in · Reset your password · Create account · Reset password · Create a new account More on forum.nim-lang.org
🌐 forum.nim-lang.org
🌐
Lenovo
lenovo.com › home
Everything You Need to Know About the Dereference Operator | Lenovo US
The dereference operator (*) and the address-of operator (&) are complementary. The address-of operator is used to obtain the memory address of a variable, while the dereference operator is used to access the value stored at a memory address pointed to by a pointer.
🌐
Reddit
reddit.com › r/compilers › why is the dereference operator generally a prefix in most langauges?
r/Compilers on Reddit: Why is the dereference operator generally a prefix in most langauges?
March 22, 2024 -

While working on my own toy programming language I had a thought: Why is the dereference operator a prefix operator (*ptr) rather than a suffix (ptr*) in most programming languages?

I guess when it comes to the classic C-style dereference operator * the parser or programmer might have a hard time telling the difference between a dereference (thing*) and the left side of a multiplication expression (thing * other_thing). However, I think that problem is really easy to solve - just use a different symbol for the dereference operator. Here's an example:

Reference a value: &value

Dereference a pointer/reference (? means dereference the preceding expression): ptr?

I think the dereference operator makes much more sense as a suffix (using a symbol that doesn't collide with others) than a prefix. Consider the following examples.

// Take a reference
let obj_ptr = &some_obj;

// Access a field on the underlying object
let field_value = obj_ptr->field; // C/C++ dereference syntax
let field_value = (*obj_ptr).field; // Rust dereference syntax

// Access a value via a pointer inside the object field
let value = *obj_ptr->ptr_field; // Two different kinds of C/C++ dereference syntax
let value = *((*obj_ptr).ptr_field); // Rust dereference syntax

What a mess! Isn't this much cleaner?

// Take a reference
let obj_ptr = &some_obj;

// Access a field on the underlying object
let field_value = obj_ptr?.field;

// Access a value via a pointer inside the object field
let value = obj_ptr?.ptr_field?;

This way, every dereference looks the same (unlike in C/C++), and it's totally clear what is being dereferenced without the need for excessive parenthesis (unlike in Rust). I know many languages like Rust and Go also do implicit dereferencing, but if you need to explicitly dereference something, I really feel like a suffix operator is the obvious way to go. What am I missing here? Why do so few language do it like this? Is it just because it's simpler to make all unary operators prefix operators rather than having some that are suffix?

Find elsewhere
Top answer
1 of 4
2

A pointer is a variable whose value is the address of another variable or object. We say that the pointer "points to" that other object.

When writing an expression, cp means the pointer variable. *cp means the variable or object whose address the pointer variable holds.

Try to keep clear in your mind the distinction between "the pointer" and "the thing being pointed to". These are two distinct things, each with their own lifetimes and storage requirements.

So to address the code in your question, if (cp) is testing the pointer. It's short for if ( cp != nullptr ), i.e. has the pointer been set to point somewhere? (In other words, does the pointer currently hold the address of another object?)

if (*cp) means if (*cp != 0) , it is asking about the value of the object whose address is stored in the pointer (in other words, the value of the object being pointed to).

2 of 4
2

The difference is simple but takes practice to get used to. Essentially, the dereference operator is used when you want to deal with what the pointer is actually pointing to (i.e the thing that is actually at the end of the pointer).

So for example, if I have a char *cp = get_string();, then cp is a pointer that is pointing to the first character in an array of characters.

Checking something like if(cp) is checking whether the pointer is 0 (i.e. if the pointer is pointing to NULL). This is probably useful in this context in that get_string() returns a pointer to a string if it is successful, or a NULL if something went wrong.

In contrast, doing something like while(*cp) is saying, while the character currently pointed to by cp is not 0 (the null character \0), then continue looping. In this context, your while loop probably looks something like:

while(*cp) {
    // process letter currently pointed to by cp
    cp++; // advance cp to point to the next character.
}

This is a common idiom to iterate through a character array.

🌐
Quora
quora.com › What-does-a-dereference-operator-do
What does a dereference operator do? - Quora
Answer: In Delphi programming language: var A : byte; //defines a variable called A residing at a certain memory addresss say $00123456 (they $ character indicates a hexadecimal number) “@” means address of, so “@A = $00123456 A := 5;’// sets value 5 in memory address $00123456 For ...
🌐
Emory
cs.emory.edu › ~cheung › Courses › 561 › Syllabus › 3-C › ref-deref.html
The reference (&) and dereference (*) operators
The reference operator & can be used to pass the address of the actual parameter · The dereference operator * can be used to obtain the value stored at any given address
🌐
Udacity
udacity.com › blog › cpp-dereferencing-explained
C++ Dereferencing Explained | Udacity
October 24, 2024 - We’ve identified how to use pointers in C++ to obtain memory locations, and we’ve looked at how to take advantage of the dereference operator (*) to tell us the value of the variable at the location we’re pointing to with our pointer.
🌐
Google Groups
groups.google.com › g › golang-nuts › c › I5cwHmFQ3u4
Pointer dereference operator precedence
Specifically, with a variable x ... are the general order of evaluation, which is left to right (not the case here), and operator precedence which states that pointer dereference is a unary operator and unary operators have highest precedence (again clearly not what's ...
🌐
Nim Forum
forum.nim-lang.org › t › 1005
Pointer dereference operator
Sign up · Log in · Reset your password · Create account · Reset password · Create a new account
Top answer
1 of 6
919

Reviewing the basic terminology

It's usually good enough - unless you're programming assembly - to envisage a pointer containing a numeric memory address, with 1 referring to the second byte in the process's memory, 2 the third, 3 the fourth and so on....

  • What happened to 0 and the first byte? Well, we'll get to that later - see null pointers below.
  • For a more accurate definition of what pointers store, and how memory and addresses relate, see "More about memory addresses, and why you probably don't need to know" at the end of this answer.

When you want to access the data/value in the memory that the pointer points to - the contents of the address with that numerical index - then you dereference the pointer.

Different computer languages have different notations to tell the compiler or interpreter that you're now interested in the pointed-to object's (current) value - I focus below on C and C++.

A pointer scenario

Consider in C, given a pointer such as p below...

Copyconst char* p = "abc";

...four bytes with the numerical values used to encode the letters 'a', 'b', 'c', and a 0 byte to denote the end of the textual data, are stored somewhere in memory and the numerical address of that data is stored in p. This way C encodes text in memory is known as ASCIIZ.

For example, if the string literal happened to be at address 0x1000 and p a 32-bit pointer at 0x2000, the memory content would be:

CopyMemory Address (hex)    Variable name    Contents
1000                                     'a' == 97 (ASCII)
1001                                     'b' == 98
1002                                     'c' == 99
1003                                     0
...
2000-2003               p                1000 hex

Note that there is no variable name/identifier for address 0x1000, but we can indirectly refer to the string literal using a pointer storing its address: p.

Dereferencing the pointer

To refer to the characters p points to, we dereference p using one of these notations (again, for C):

Copyassert(*p == 'a');  // The first character at address p will be 'a'
assert(p[1] == 'b'); // p[1] actually dereferences a pointer created by adding
                     // p and 1 times the size of the things to which p points:
                     // In this case they're char which are 1 byte in C...
assert(*(p + 1) == 'b');  // Another notation for p[1]

You can also move pointers through the pointed-to data, dereferencing them as you go:

Copy++p;  // Increment p so it's now 0x1001
assert(*p == 'b');  // p == 0x1001 which is where the 'b' is...

If you have some data that can be written to, then you can do things like this:

Copyint x = 2;
int* p_x = &x;  // Put the address of the x variable into the pointer p_x
*p_x = 4;       // Change the memory at the address in p_x to be 4
assert(x == 4); // Check x is now 4

Above, you must have known at compile time that you would need a variable called x, and the code asks the compiler to arrange where it should be stored, ensuring the address will be available via &x.

Dereferencing and accessing a structure data member

In C, if you have a variable that is a pointer to a structure with data members, you can access those members using the -> dereferencing operator:

Copytypedef struct X { int i_; double d_; } X;
X x;
X* p = &x;
p->d_ = 3.14159;  // Dereference and access data member x.d_
(*p).d_ *= -1;    // Another equivalent notation for accessing x.d_

Multi-byte data types

To use a pointer, a computer program also needs some insight into the type of data that is being pointed at - if that data type needs more than one byte to represent, then the pointer normally points to the lowest-numbered byte in the data.

So, looking at a slightly more complex example:

Copydouble sizes[] = { 10.3, 13.4, 11.2, 19.4 };
double* p = sizes;
assert(p[0] == 10.3);  // Knows to look at all the bytes in the first double value
assert(p[1] == 13.4);  // Actually looks at bytes from address p + 1 * sizeof(double)
                       // (sizeof(double) is almost always eight bytes)
++p;                   // Advance p by sizeof(double)
assert(*p == 13.4);    // The double at memory beginning at address p has value 13.4
*(p + 2) = 29.8;       // Change sizes[3] from 19.4 to 29.8
                       // Note earlier ++p and + 2 here => sizes[3]

Pointers to dynamically allocated memory

Sometimes you don't know how much memory you'll need until your program is running and sees what data is thrown at it... then you can dynamically allocate memory using malloc. It is common practice to store the address in a pointer...

Copyint* p = (int*)malloc(sizeof(int)); // Get some memory somewhere...
*p = 10;            // Dereference the pointer to the memory, then write a value in
fn(*p);             // Call a function, passing it the value at address p
(*p) += 3;          // Change the value, adding 3 to it
free(p);            // Release the memory back to the heap allocation library

In C++, memory allocation is normally done with the new operator, and deallocation with delete:

Copyint* p = new int(10); // Memory for one int with initial value 10
delete p;

p = new int[10];      // Memory for ten ints with unspecified initial value
delete[] p;

p = new int10;    // Memory for ten ints that are value initialised (to 0)
delete[] p;

See also C++ smart pointers below.

Losing and leaking addresses

Often a pointer may be the only indication of where some data or buffer exists in memory. If ongoing use of that data/buffer is needed, or the ability to call free() or delete to avoid leaking the memory, then the programmer must operate on a copy of the pointer...

Copyconst char* p = asprintf("name: %s", name);  // Common but non-Standard printf-on-heap

// Replace non-printable characters with underscores....
for (const char* q = p; *q; ++q)
    if (!isprint(*q))
        *q = '_';

printf("%s\n", p); // Only q was modified
free(p);

...or carefully orchestrate reversal of any changes...

Copyconst size_t n = ...;
p += n;
...
p -= n;  // Restore earlier value...
free(p);

C++ smart pointers

In C++, it's best practice to use smart pointer objects to store and manage the pointers, automatically deallocating them when the smart pointers' destructors run. Since C++11 the Standard Library provides two, unique_ptr for when there's a single owner for an allocated object...

Copy{
    std::unique_ptr<T> p{new T(42, "meaning")};
    call_a_function(p);
    // The function above might throw, so delete here is unreliable, but...
} // p's destructor's guaranteed to run "here", calling delete

...and shared_ptr for share ownership (using reference counting)...

Copy{
    auto p = std::make_shared<T>(3.14, "pi");
    number_storage1.may_add(p); // Might copy p into its container
    number_storage2.may_add(p); // Might copy p into its container    } // p's destructor will only delete the T if neither may_add copied it

Null pointers

In C, NULL and 0 - and additionally in C++ nullptr - can be used to indicate that a pointer doesn't currently hold the memory address of a variable, and shouldn't be dereferenced or used in pointer arithmetic. For example:

Copyconst char* p_filename = NULL; // Or "= 0", or "= nullptr" in C++
int c;
while ((c = getopt(argc, argv, "f:")) != -1)
    switch (c) {
      case f: p_filename = optarg; break;
    }
if (p_filename)  // Only NULL converts to false
    ...   // Only get here if -f flag specified

In C and C++, just as inbuilt numeric types don't necessarily default to 0, nor bools to false, pointers are not always set to NULL. All these are set to 0/false/NULL when they're static variables or (C++ only) direct or indirect member variables of static objects or their bases, or undergo zero initialisation (e.g. new T(); and new T(x, y, z); perform zero-initialisation on T's members including pointers, whereas new T; does not).

Further, when you assign 0, NULL and nullptr to a pointer the bits in the pointer are not necessarily all reset: the pointer may not contain "0" at the hardware level, or refer to address 0 in your virtual address space. The compiler is allowed to store something else there if it has reason to, but whatever it does - if you come along and compare the pointer to 0, NULL, nullptr or another pointer that was assigned any of those, the comparison must work as expected. So, below the source code at the compiler level, "NULL" is potentially a bit "magical" in the C and C++ languages...

More about memory addresses, and why you probably don't need to know

More strictly, initialised pointers store a bit-pattern identifying either NULL or a (often virtual) memory address.

The simple case is where this is a numeric offset into the process's entire virtual address space; in more complex cases the pointer may be relative to some specific memory area, which the CPU may select based on CPU "segment" registers or some manner of segment id encoded in the bit-pattern, and/or looking in different places depending on the machine code instructions using the address.

For example, an int* properly initialised to point to an int variable might - after casting to a float* - access memory in "GPU" memory quite distinct from the memory where the int variable is, then once cast to and used as a function pointer it might point into further distinct memory holding machine opcodes for the program (with the numeric value of the int* effectively a random, invalid pointer within these other memory regions).

3GL programming languages like C and C++ tend to hide this complexity, such that:

  • If the compiler gives you a pointer to a variable or function, you can dereference it freely (as long as the variable's not destructed/deallocated meanwhile) and it's the compiler's problem whether e.g. a particular CPU segment register needs to be restored beforehand, or a distinct machine code instruction used

  • If you get a pointer to an element in an array, you can use pointer arithmetic to move anywhere else in the array, or even to form an address one-past-the-end of the array that's legal to compare with other pointers to elements in the array (or that have similarly been moved by pointer arithmetic to the same one-past-the-end value); again in C and C++, it's up to the compiler to ensure this "just works"

  • Specific OS functions, e.g. shared memory mapping, may give you pointers, and they'll "just work" within the range of addresses that makes sense for them

  • Attempts to move legal pointers beyond these boundaries, or to cast arbitrary numbers to pointers, or use pointers cast to unrelated types, typically have undefined behaviour, so should be avoided in higher level libraries and applications, but code for OSes, device drivers, etc. may need to rely on behaviour left undefined by the C or C++ Standard, that is nevertheless well defined by their specific implementation or hardware.

2 of 6
134

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.

Copyint a = 10;
int* ptr = &a;

printf("%d", *ptr); // With *ptr I'm dereferencing the pointer. 
                    // Which means, I am asking the value pointed at by the pointer.
                    // ptr is pointing to the location in memory of the variable a.
                    // In a's location, we have 10. So, dereferencing gives this value.

// Since we have indirect control over a's location, we can modify its content using the pointer. This is an indirect way to access a.

 *ptr = 20;         // Now a's content is no longer 10, and has been modified to 20.
🌐
Ziggit
ziggit.dev › explain
Why use a different pointer dereference operator than C? - Explain - Ziggit
May 8, 2024 - One of the things I find slightly awkward about Zig is the use of the .* operator to dereference pointers. My question is, why is this so? Are there any advantages of this vs. C’s prefix * operator?
🌐
Scaler
scaler.com › home › topics › c dereference pointer
C Dereference pointer- Scaler Topics
May 30, 2023 - By using this operator, the address of the variable (on which we are working) is assigned to the pointer variable. Dereferencing is used to access or find out the data which is contained in the memory location which is pointed by the pointer.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_dereference_pointer.htm
Dereference Pointer in C
The dereference operator is used to access and manipulate the value stored in the variable pointed by the pointer. The dereference or indirection operator (*) acts as a unary operator, and it needs a pointer variable as its operand.
🌐
The Rust Programming Language
doc.rust-lang.org › book › ch04-02-references-and-borrowing.html
References and Borrowing - The Rust Programming Language
Note: The opposite of referencing by using & is dereferencing, which is accomplished with the dereference operator, *. We’ll see some uses of the dereference operator in Chapter 8 and discuss details of dereferencing in Chapter 15.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › cpp-dereferencing
C++ Dereferencing - GeeksforGeeks
October 27, 2025 - Dereferencing is, use of a pointer to access the value whose address is being stored. We use the * operator to get the value of the variable from its address.