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

Does dereferencing a struct return a new copy of struct?

Why when we reference struct using (*structObj) does Go seem to return a new copy of structObj rather than return the same address of original structObj? This might be some misunderstanding of mine, so I seek clarification package main import ( …
ken
  • 13,869
  • 6
  • 42
  • 36
18
votes
4 answers

What exactly is the purpose of the (asterisk) in pointers?

I'm new to programming and I'm trying to wrap my head around the idea of 'pointers'. int main() { int x = 5; int *pointerToInteger = & x; cout<
iansmathew
  • 367
  • 1
  • 3
  • 13
18
votes
6 answers

Pointer operations and operator precedence in C

Background Just had a chat with a C guy today and we disagreed on the following: int intgA[2] = { 1, 2 }; int intgB[2] = { 3, 5 }; int *intAPtr = intgA; int *intBPtr = intgB; So when we do: *intAPtr++ = *intBPtr++; My analysis First: intBPtr…
Unheilig
  • 16,196
  • 193
  • 68
  • 98
17
votes
3 answers

Difference between pointer to pointer and pointer to array?

Given that the name of an array is actually a pointer to the first element of an array, the following code: #include int main(void) { int a[3] = {0, 1, 2}; int *p; p = a; printf("%d\n", p[1]); return 0; } prints 1,…
jpmelos
  • 3,283
  • 3
  • 23
  • 30
17
votes
6 answers

Why is there no safe alternative to unique_ptr::operator*()?

std::vector has the member function at() as a safe alternative to operator[], so that bound checking is applied and no dangling references are created: void foo(std::vector const&x) { const auto&a=x[0]; // What if x.empty()? Undefined…
Walter
  • 44,150
  • 20
  • 113
  • 196
16
votes
1 answer

What exactly does mean the term "dereferencing" an object?

I'm reading the description of the new feature in C# 8 called nullable reference types. The description discusses so called null-forgiving operator. The example in the description talks about de-referencing of an instance of a reference type (I…
usr2020
  • 324
  • 2
  • 8
16
votes
3 answers

Dereferencing strings and HashMaps in Rust

I'm trying to understand how HashMaps work in Rust and I have come up with this example. use std::collections::HashMap; fn main() { let mut roman2number: HashMap<&'static str, i32> = HashMap::new(); roman2number.insert("X", 10); …
skanur
  • 175
  • 2
  • 8
16
votes
3 answers

What is dereferencing possible null pointer?

I am making a program for SFTP in NetBeans. Some part of My code: com.jcraft.jsch.Session sessionTarget = null; com.jcraft.jsch.ChannelSftp channelTarget = null; try { sessionTarget = jsch.getSession(backupUser, backupHost, backupPort); …
earthmover
  • 4,395
  • 10
  • 43
  • 74
16
votes
4 answers

Is it considered good style to dereference `new` pointer?

To avoid keep having to use -> and instead work directly with the object, is it acceptable practice to do: obj x = *(new obj(...)); ... delete &obj;
mchen
  • 9,808
  • 17
  • 72
  • 125
15
votes
1 answer

When to dereference or not

I am going through the "Rust Book" website in order to learn the language for an upcoming job interview. In the chapter on vectors, there are two code samples: fn main() { let v = vec![100, 32, 57]; for i in &v { println!("{}", i); …
Austin Wile
  • 181
  • 1
  • 5
15
votes
2 answers

Why does taking a member function pointer value requires class name qualification even from inside of the class?

When returning a member function pointer to a class within one of that class's member functions I still have to specify the class. I cannot simply take the address. For example, this code works fine: class Foo { public: void func(int param) {…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
14
votes
2 answers

Why can't I dereference a pointer to an object that's an array-element using the indirection operator?

Is it not possible to dereference a pointer to an object that's stored in an array using the indirection(dereference) operator or am I doing something wrong? #include class A { public: virtual void test() { …
user4087080
14
votes
2 answers

Safe pointer dereferencing in C++

In our code base we have many constructions like this: auto* pObj = getObjectThatMayVeryRarelyBeNull(); if (!pObj) throw std::runtime_error("Ooops!"); // Use pObj->(...) In 99.99% of cases this check is not triggered. I'm thinking about the…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
14
votes
5 answers

char and char* (pointer)

I would like to understand how pointers work, so i created this small program. first of all i create a p pointer, which points to a char. The first question is at this point. If i create a pointer, the value of it is a memoryaddress (if i point it…
Iburidu
  • 450
  • 2
  • 5
  • 15
13
votes
3 answers

Generally, is dereference pointer expression results a reference type?

Deferencing pointer leads to using the value of the object indirectly. But I've never really understood what does the "using" means. I started to think the question until my compiler yield an error for the following code int i = 0, *pi =…
SLN
  • 4,772
  • 2
  • 38
  • 79
1 2
3
78 79