Questions tagged [dereference]

Anything related to pointer dereference, i.e. the process of determining the object which the pointer is referring to. Languages having pointer variables usually have a special operator to perform dereferencing of pointers (e.g. in C and C++, if `p` is a valid pointer, `*p` is the object pointed to by `p`).

Anything related to pointer dereference, i.e. the process of determining the object which the pointer is referring to. Languages having pointer variables usually have a special operator to perform dereferencing of pointers (e.g. in C and C++, if p is a valid pointer, *p is the object pointed to by p).

1184 questions
683
votes
6 answers

What does 'dereferencing' a pointer mean in C/C++?

Please include an example with the explanation.
asir
  • 12,843
  • 8
  • 24
  • 18
330
votes
4 answers

Why does the arrow (->) operator in C exist?

The dot (.) operator is used to access a member of a struct, while the arrow operator (->) in C is used to access a member of a struct which is referenced by the pointer in question. The pointer itself does not have any members which could be…
Askaga
  • 6,061
  • 5
  • 25
  • 49
87
votes
3 answers

What do the ampersand '&' and star '*' symbols mean in Rust?

Despite thoroughly reading the documentation, I'm rather confused about the meaning of the & and * symbol in Rust, and more generally about what is a Rust reference exactly. In this example, it seems to be similar to a C++ reference (that is, an…
John Smith Optional
  • 22,259
  • 12
  • 43
  • 61
81
votes
7 answers

Meaning of "referencing" and "dereferencing" in C

I read different things on the Internet and got confused, because every website says different things. I read about * referencing operator and & dereferencing operator; or that referencing means making a pointer point to a variable and dereferencing…
Milkncookiez
  • 6,817
  • 10
  • 57
  • 96
80
votes
2 answers

org.hibernate.QueryException: illegal attempt to dereference collection

I am trying following hql query to execute SELECT count(*) FROM BillDetails as bd WHERE bd.billProductSet.product.id = 1002 AND bd.client.id = 1 But it is showing org.hibernate.QueryException: illegal attempt to dereference…
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
77
votes
13 answers

Checking if an iterator is valid

Is there any way to check if an iterator (whether it is from a vector, a list, a deque...) is (still) dereferenceable, i.e. has not been invalidated? I have been using try-catch, but is there a more direct way to do this? Example: (which doesn't…
huff
  • 1,954
  • 2
  • 28
  • 49
76
votes
2 answers

Dereference vector pointer to access element

If i have in C++ a pointer to a vector: vector* vecPtr; And i'd like to access an element of the vector, then i can do this by dereferncing the vector: int a = (*vecPtr)[i]; but will this dereferencing actually create a copy of my vector on…
Mat
  • 11,263
  • 10
  • 45
  • 48
71
votes
7 answers

dereferencing pointer to incomplete type

I've seen a lot of questions on this but I'm going to ask the question differently without specific code. Is there a way of EASILY determining what is causing the type to be incomplete? In my case I'm using someone elses code and I'm completely…
nick
  • 1,055
  • 1
  • 10
  • 17
69
votes
2 answers

Is it considered a bad practice to implement Deref for newtypes?

I often use the newtype pattern, but I am tired of writing my_type.0.call_to_whatever(...). I am tempted to implement the Deref trait because it permits writing simpler code since I can use my newtype as if it were the underlying type in some…
Boiethios
  • 38,438
  • 19
  • 134
  • 183
69
votes
2 answers

Why does printing a pointer print the same thing as printing the dereferenced pointer?

From the Rust guide: To dereference (get the value being referred to rather than the reference itself) y, we use the asterisk (*) So I did it: fn main() { let x = 1; let ptr_y = &x; println!("x: {}, ptr_y: {}", x, *ptr_y); } This…
Vega
  • 2,661
  • 5
  • 24
  • 49
64
votes
6 answers

Why is the dereference operator (*) also used to declare a pointer?

I'm not sure if this is a proper programming question, but it's something that has always bothered me, and I wonder if I'm the only one. When initially learning C++, I understood the concept of references, but pointers had me confused. Why, you ask?…
diggingforfire
  • 3,359
  • 1
  • 23
  • 33
57
votes
1 answer

Why would code explicitly call a static method via a null pointer?

I've seen code like this in a couple of old projects: class Class { static void Method() {} }; ((Class*)0)->Method(); This code contains undefined behavior because it includes dereferencing a null pointer (no matter what happens afterwards).…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
55
votes
5 answers

Dereferencing a pointer to 0 in C

Sometimes data at memory address 0x0 is quite valuable -- take x86 real mode IVT as a more known example: it starts at 0x0 and contains pointers to interrupt handlers: a dword at 0x00 is a pointer to division by zero error handler. However, C11…
gfv
  • 784
  • 7
  • 12
53
votes
4 answers

Does dereferencing a pointer make a copy of it?

Does dereferencing a pointer and passing that to a function which takes its argument by reference create a copy of the object?
wrongusername
  • 18,564
  • 40
  • 130
  • 214
49
votes
10 answers

How to understand the pointer star * in C?

I'm struggling with the pointer sign *, I find it very confusing in how it's used in both declarations and expressions. For example: int *i; // i is a pointer to an int But what is the logic behind the syntax? What does the * just before the i…
Jeffrey
  • 493
  • 1
  • 5
  • 4
1
2 3
78 79