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
8
votes
3 answers

Is there a reason to use *& or &* in C code?

I've come across some C code that make use of the reference/dereference (or whatever you choose to call them) operators, * and &, at the same time, like &*foo and *&bar. I'm puzzled by it. Is there any reason to do this?
qsfzy
  • 554
  • 5
  • 17
8
votes
2 answers

Why does Autovivification occur with keys() and not %{..}?

This is a subtlety I found with keys(). $ perl -e 'use warnings; use strict; my $d = { "ab" => 1 }; my @e = keys(%{$d->{cd}});' $ perl -e 'use warnings; use strict; my $d = { "ab" => 1 }; my %e = %{$d->{cd}};' Can't use an undefined value as a…
8
votes
1 answer

Pointer to an Element of a Vector

If I have a pointer that is pointing to an element in a vector, say element 2, and then that element gets swapped with element 4 of the same vector. Is the pointer now pointing to element 2, element 4, or neither? Example: vector a is equal to…
Silvae
  • 594
  • 1
  • 8
  • 20
8
votes
2 answers

Dereference arbitrary memory location in C

I'm trying to debug a program I've written. According to the debugger a particular void * holds the value 0x804b008. I'd like to be able to dereference this value (cast it to an int * and get it's value). I'm getting a Segmentation Error with this…
Tyler
  • 4,679
  • 12
  • 41
  • 60
8
votes
1 answer

Why can't I treat an array like a pointer in C?

I see this question a lot on SO. Maybe not in so many words... but time and again there is confusion on how arrays are different from pointers. So I thought I would take a moment to Q&A a few points about this. For purposes of this Q&A we're going…
Mike
  • 47,263
  • 29
  • 113
  • 177
8
votes
1 answer

C++ - Get value of a particular memory address

I was wondering whether it is possible to do something like this: unsigned int address = 0x0001FBDC; // Random address :P int value = *address; // Dereference of address Meaning, is it possible to get the value of a particular address in memory…
Zyyk Savvins
  • 483
  • 4
  • 8
  • 14
7
votes
3 answers

Dereferencing values from an array to declared variables in one line

To retrieve arguments from a function call I usually do use strict; use warnings; foo([1,2],[3,4]); sub foo{ my ($x, $y) = @_; ... } In the example, $x and $y are now references to an array each. If I want to use the variables inside…
MattLBeck
  • 5,701
  • 7
  • 40
  • 56
7
votes
1 answer

Dereferencing a shared pointer and assigning to it

Is it ok to dereference a shared pointer, assign and assign a new object to it like so: void foo() { std::shared_ptr x = std::make_shared(); bar(*x); // is this fine? // x ==…
Martin F
  • 171
  • 1
  • 1
  • 10
7
votes
1 answer

String equality in Rust: how does referencing and dereferencing work?

As a Rust newbie, I'm working through the Project Euler problems to help me get a feel for the language. Problem 4 deals with palindromes, and I found two solutions for creating a vector of palindromes, but I'm not sure how either of them work. I'm…
piercebot
  • 1,767
  • 18
  • 16
7
votes
1 answer

Why doesn't a nested reference to an array coerce to a slice?

I read What are Rust's exact auto-dereferencing rules? from beginning to end, but I still have a question about the coercion from array to slice. Let us think about the following code: let arr: &[i32; 5] = &&&[1, 2, 3, 4, 5]; // let arr: &[i32] =…
7
votes
1 answer

Why are the 'dereference' and the 'address of' operators on the left?

In C (and some other C-like languages) we have 2 unary operators for working with pointers: the dereference operator (*) and the 'address of' operator (&). They are left unary operators, which introduces an uncertainty in order of operations, for…
RuRo
  • 311
  • 4
  • 17
7
votes
2 answers

ClojureScript Re-frame subscription dereferencing dilemma

What's the best of following approaches? Outer subscription, early deref (defn component [msg] [:p msg])) (let [msg (rf/subscribe [:msg])] [component @msg] Outer subscription, late deref (defn component [msg] [:p @msg])) (let [msg…
jsmesami
  • 128
  • 1
  • 6
7
votes
4 answers

Perl: How do you dereference an array without creating a copy of the array?

When I dereference an array using @$arrayRef or @{$arrayRef} it appears to create a copy of the array. Is there a correct way to dereference an array? This code... sub updateArray1 { my $aRef = shift; my @a = @$aRef; my…
DemiImp
  • 968
  • 8
  • 18
7
votes
5 answers

C structure pointer dereferencing speed

I have a question regarding the speed of pointer dereferencing. I have a structure like so: typedef struct _TD_RECT TD_RECT; struct _TD_RECT { double left; double top; double right; double bottom; }; My question is, which of these would be…
oldSkool
  • 1,212
  • 5
  • 14
  • 29
7
votes
3 answers

In C++, do dereferencing and getting index zero do the same tihng?

I just tried this code: int i = 33; int * pi = &i; cout << "i: " << *pi << endl; cout << "i: " << pi[0] << endl; Both lines return the same thing. Essentially, if I get index zero of any pointer, I'll get the value of the correct type at the…
eje211
  • 2,385
  • 3
  • 28
  • 44