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

How to cast/convert pointer to reference in C++

How can I pass a pointer (Object *ob) to a function which prototype is void foo(Object &) ?
Dewsworld
  • 13,367
  • 23
  • 68
  • 104
193
votes
4 answers

How do I do a literal *int64 in Go?

I have a struct type with a *int64 field. type SomeType struct { SomeField *int64 } At some point in my code, I want to declare a literal of this (say, when I know said value should be 0, or pointing to a 0, you know what I mean) instance :=…
ThisGuy
  • 2,661
  • 2
  • 18
  • 18
184
votes
18 answers

Is it good practice to NULL a pointer after deleting it?

I'll start out by saying, use smart pointers and you'll never have to worry about this. What are the problems with the following code? Foo * p = new Foo; // (use p) delete p; p = NULL; This was sparked by an answer and comments to another question.…
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
178
votes
29 answers

What do people find difficult about C pointers?

From the number of questions posted here, it's clear that people have some pretty fundemental issues when getting their heads around pointers and pointer arithmetic. I'm curious to know why. They've never really caused me major problems (although I…
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
170
votes
6 answers

In C, why do some people cast the pointer before freeing it?

I'm working on an old code base and pretty much every invocation of free() uses a cast on its argument. For example, free((float *)velocity); free((float *)acceleration); free((char *)label); where each pointer is of the corresponding (and…
SO Stinks
  • 3,258
  • 4
  • 32
  • 37
168
votes
10 answers

Should I store entire objects, or pointers to objects in containers?

Designing a new system from scratch. I'll be using the STL to store lists and maps of certain long-live objects. Question: Should I ensure my objects have copy constructors and store copies of objects within my STL containers, or is it generally…
Stéphane
  • 19,459
  • 24
  • 95
  • 136
165
votes
6 answers

Declaring pointers; asterisk on the left or right of the space between the type and name?

Possible Duplicates: What makes more sense - char* string or char *string? Pointer declarations in C++: placement of the asterisk I've seen mixed versions of this in a lot of code. (This applies to C and C++, by the way.) People seem to declare…
jakogut
  • 4,409
  • 6
  • 29
  • 41
164
votes
12 answers

Function Pointers in Java

This may be something common and trivial, but I seem to be having trouble finding a concrete answer. In C# there is a concept of delegates, which relates strongly to the idea of function pointers from C++. Is there a similar functionality in Java?…
dreadwail
  • 15,098
  • 21
  • 65
  • 96
162
votes
8 answers

Why do C++ libraries and frameworks never use smart pointers?

I read in a few articles that raw pointers should almost never be used. Instead they should always be wrapped inside smart pointers, whether it's scoped or shared pointers. However, I noticed that frameworks like Qt, wxWidgets and libraries like…
laurent
  • 88,262
  • 77
  • 290
  • 428
159
votes
3 answers

What does `dword ptr` mean?

Could someone explain what this means? (Intel Syntax, x86, Windows) and dword ptr [ebp-4], 0
小太郎
  • 5,510
  • 6
  • 37
  • 48
158
votes
14 answers

C: differences between char pointer and array

Consider: char amessage[] = "now is the time"; char *pmessage = "now is the time"; I read from The C Programming Language, 2nd Edition that the above two statements don't do the same thing. I always thought that an array is an convenient way to…
Midnight Blue
  • 5,061
  • 10
  • 41
  • 46
157
votes
17 answers

What's the point of const pointers?

I'm not talking about pointers to const values, but const pointers themselves. I'm learning C and C++ beyond the very basic stuff and just until today I realized that pointers are passed by value to functions, which makes sense. This means that…
R. Ruiz.
  • 1,673
  • 3
  • 12
  • 11
157
votes
10 answers

C pointers : pointing to an array of fixed size

This question goes out to the C gurus out there: In C, it is possible to declare a pointer as follows: char (* p)[10]; .. which basically states that this pointer points to an array of 10 chars. The neat thing about declaring a pointer like this is…
figurassa
  • 2,037
  • 2
  • 13
  • 12
156
votes
11 answers

Pointer expressions: *ptr++, *++ptr and ++*ptr

Recently I have come across this problem which I am unable to understand by myself. What do these three Expressions REALLY mean? *ptr++ *++ptr ++*ptr I have tried Ritchie. But unfortunately was unable to follow what he told about these 3…
allocated
  • 1,295
  • 3
  • 13
  • 16
154
votes
11 answers

constant pointer vs pointer on a constant value

What is the difference between the following declarations? char * const a; const char * a; In order to understand the difference I wrote this small program: #include #include int main (int argc, char **argv) { char a =…
rahmu
  • 5,708
  • 9
  • 39
  • 57