Questions tagged [pointers]

Data types for "pointing" at other values: A pointer's value is a memory address where the pointed-to value is stored. This tag should be used for questions involving the use of pointers, not references. Common programming languages using pointers are C, C++, Go, and assembly and intermediate-representation languages; use a specific language tag. Other helpful tags should describe what is being pointed-to (e.g. a function, a struct etc.)

A pointer is a data type that "points to" another value stored in memory using its address. Using a pointer rather than the pointed-to entity often holds performance benefits in repetitive operations. For example, copying a pointer is very often "cheaper" than copying the value that it points to: The pointee may be a large data structure, or its copying may be non-trivial, involving memory-location dependent values, while with a pointer - only the address must be copied.

Pointers are an important concept in many high-level programming languages, including C and C++. The Wikipedia page on pointers has a fairly in-depth introduction to the concept.

Please consult some of the following content for a more in-depth explanation of pointers.

Books

See also

56155 questions
153
votes
11 answers

Pointers in Python?

I know Python doesn't have pointers, but is there a way to have this yield 2 instead >>> a = 1 >>> b = a # modify this line somehow so that b "points to" a >>> a = 2 >>> b 1 ? Here's an example: I want form.data['field'] and form.field.value to…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
151
votes
10 answers

Passing references to pointers in C++

As far as I can tell, there's no reason I shouldn't be allowed to pass a reference to a pointer in C++. However, my attempts to do so are failing, and I have no idea why. This is what I'm doing: void myfunc(string*& val) { // Do stuff to the…
Alex
  • 14,973
  • 13
  • 59
  • 94
150
votes
1 answer

What is a "fat pointer"?

I've read the term "fat pointer" in several contexts already, but I'm not sure what exactly it means and when it is used in Rust. The pointer seems to be twice as large as a normal pointer, but I don't understand why. It also seems to have something…
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
149
votes
12 answers

Why is x[0] != x[0][0] != x[0][0][0]?

I'm studying a little of C++ and I'm fighting with pointers. I understand that I can have 3 level of pointers by declaring: int *(*x)[5]; so that *x is a pointer to an array of 5 elements that are pointers to int. Also I know that x[0] = *(x+0);,…
Leo91
  • 1,741
  • 3
  • 13
  • 20
148
votes
16 answers

How does delete[] know it's an array?

Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed, void deleteForMe(int* pointer) { delete[] pointer; } The pointer could be all sorts of different things, and so performing an…
GRB
  • 1,811
  • 3
  • 14
  • 10
147
votes
11 answers

Does free(ptr) where ptr is NULL corrupt memory?

Theoretically I can say that free(ptr); free(ptr); is a memory corruption since we are freeing the memory which has already been freed. But what if free(ptr); ptr=NULL; free(ptr); As the OS will behave in an undefined manner I cannot get an…
Vijay
  • 65,327
  • 90
  • 227
  • 319
147
votes
17 answers

How can I use pointers in Java?

I know Java doesn't have pointers, but I heard that Java programs can be created with pointers and that this can be done by the few who are experts in Java. Is it true?
user213038
145
votes
4 answers

What is the difference between std::reference_wrapper and a simple pointer?

Why is there a need to have std::reference_wrapper? Where should it be used? How is it different from a simple pointer? How its performance compares to a simple pointer?
Laurynas Lazauskas
  • 2,855
  • 2
  • 21
  • 26
143
votes
16 answers

Pointer to pointer clarification

I was following this tutorial about how does a pointer to a pointer work. Let me quote the relevant passage: int i = 5, j = 6, k = 7; int *ip1 = &i, *ip2 = &j; Now we can set int **ipp = &ip1; and ipp points to ip1 which points to…
Blake
  • 7,367
  • 19
  • 54
  • 80
143
votes
8 answers

Range references instead values

I saw that range returns the key and the "copy" of the value. Is there a way for that range to return the address of the item? Example package main import "fmt" type MyType struct { field string } func main() { var array [10]MyType …
Titouan56
  • 6,932
  • 11
  • 37
  • 61
141
votes
23 answers

How to explain C pointers (declaration vs. unary operators) to a beginner?

I have had the recent pleasure to explain pointers to a C programming beginner and stumbled upon the following difficulty. It might not seem like an issue at all if you already know how to use pointers, but try to look at the following example with…
armin
  • 2,047
  • 3
  • 17
  • 19
137
votes
9 answers

Why does the use of 'new' cause memory leaks?

I learned C# first, and now I'm starting with C++. As I understand, operator new in C++ is not similar to the one in C#. Can you explain the reason of the memory leak in this sample code? class A { ... }; struct B { ... }; A *object1 = new A(); B…
user1131997
137
votes
7 answers

What can I use instead of the arrow operator, `->`?

What is the arrow operator (->) a synonym for?
P-A
  • 10,882
  • 8
  • 26
  • 19
137
votes
14 answers

Why are function pointers and data pointers incompatible in C/C++?

I have read that converting a function pointer to a data pointer and vice versa works on most platforms but is not guaranteed to work. Why is this the case? Shouldn't both be simply addresses into main memory and therefore be compatible?
gexicide
  • 38,535
  • 21
  • 92
  • 152
137
votes
10 answers

Create a pointer to two-dimensional array

I need a pointer to a static 2-dimensional array. How is this done? static uint8_t l_matrix[10][20]; void test(){ uint8_t **matrix_ptr = l_matrix; //wrong idea } I get all kinds of errors like: warning: assignment from incompatible pointer…
Dill
  • 1,943
  • 4
  • 20
  • 28