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
7
votes
2 answers

Why printf( "%s" , ptr ) is able to dereference a void*?

When we talk about dereference, is it necessary that * should be used in it? If we access the referent of the pointer in some other way, can it be considered as dereferencing a pointer or not, like: char *ptr = "abc" ; printf( "%c" , *ptr ); //…
Andrew-Dufresne
  • 5,464
  • 7
  • 46
  • 68
7
votes
4 answers

Understanding pointers with a swap program in C

I am trying to better understand pointers and referencing in C, and my course provided the following program as an example. #include void swap(int* a, int* b); int main(void) { int x = 1; int y = 2; swap(&x, &y); …
Caleb Jay
  • 2,159
  • 3
  • 32
  • 66
7
votes
1 answer

Swift pointer arithmetic and dereferencing; converting some C-like map code to Swift

I have this little bit of Swift code that does not seem to be working... // earlier, in Obj C... typedef struct _Room { uint8_t *map; int width; int height; } Room; A Room is part of a stab at a roguelike game if…
Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
7
votes
4 answers

What is the difference between scanf("%d", *p) and scanf("%d", p)?

Pointers are a new thing for me and I'm struggling to understand it, but I won't give in and hopefully learn it. What would be the difference between scanf ("%d", *p) and scanf ("%d", p)? In examples I saw that if I want to input some value in a…
Nick
  • 205
  • 1
  • 7
7
votes
5 answers

How do I dereference a Perl hash reference that's been passed to a subroutine?

I'm still trying to sort out my hash dereferencing. My current problem is I am now passing a hashref to a sub, and I want to dereference it within that sub. But I'm not finding the correct method/syntax to do it. Within the sub, I want to iterate…
sgsax
  • 393
  • 1
  • 4
  • 8
7
votes
5 answers

How do I dereference a hash that's been returned from a method of a class?

I have a class with a method that returns a hash. Ordinarily, I would get the result like so: %resp = $myclass->sub($foo); And then access members of the returned hash like this: $resp{key}{subkey}; in the case of a 2d hash. I figure there must…
sgsax
  • 393
  • 1
  • 4
  • 8
7
votes
2 answers

How to determine if a type is dereferenceable in C++03?

In C++03, how do I determine if a type T is dereferenceable? By which I mean, how do I statically determine if *t would be a valid expression for t of type T? My attempt: template struct enable_if { }; template
user541686
  • 205,094
  • 128
  • 528
  • 886
7
votes
4 answers

proper usage of the pre-increment operator in combination with the pointer dereference operator

I just wrote the following line of code: if (++(data_ptr->count) > threshold) { /*...*/ } // example 1 My intent is to increment the count variable within the data structure that data_ptr points to before making the comparison to threshold, and…
e.James
  • 116,942
  • 41
  • 177
  • 214
7
votes
2 answers

How expensive is it to dereference an array ref in Perl?

I'm curious if Perl internals creates a copy of the ref values to create the array? For example, the following outputs the last and first value of a delimited string: say @{[ split( q{\|}, q{bar|is|foo} ) ]}[-1,0]; # STDOUT: foobar\n Does the…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
7
votes
1 answer

Ruby - FileUtils - dereference_root option

Can someone explain me exactly (better if with examples) the meaning of the dereference_root option in FileUtils.cp_r and in other class method of the same class? Thank you in advance.
Astorre
  • 75
  • 4
7
votes
3 answers

naming pointers as 'pointerToXYZ'

When I'm using pointers in my code, I don't always remember whether I'm dealing with pointers or the objects they point to. So, I want to call my pointers 'pointerToXYZ'. Do any of the official style guides (Links to official style guides) describe…
tir38
  • 9,810
  • 10
  • 64
  • 107
7
votes
2 answers

Is size of char * same as size of int *?

I know: char * is a pointer to char. and int * is a pointer to int. So, i want to confirm following two things: So now suppose I am on 32 bit machine, then that means memory addresses are 32 bit wide. Thus that means size of char * and int * is…
user1599964
  • 860
  • 2
  • 13
  • 29
6
votes
3 answers

Understanding Pointer-to-Member operators

I copied this program from a c++ practice book. What's going on behind the scenes? The expected output is: sum=30 sum=70 #include using namespace std; class M { int x; int y; public: void set_xy(int a, int b) { …
user1103138
6
votes
2 answers

Print a string from ArrayList of String[]?

I have an ArrayList full of strings arrays that I built like this: String[] words = new String[tokens.length]; I have three arrays like above in my…
user961627
  • 12,379
  • 42
  • 136
  • 210
6
votes
4 answers

Dereferencing Perl hashrefs

Using Text::Ngram I have my $c = ngram_counts($text, 3); my %ct = %($c); which doesn't work (Scalar found where operator expected). I think this is a combination of not knowing what I'm doing (still not very good with Perl) and being confused…
Charles
  • 11,269
  • 13
  • 67
  • 105