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
485
votes
11 answers

What is array to pointer decay?

What is array to pointer decay? Is there any relation to array pointers?
Vamsi
  • 5,853
  • 6
  • 29
  • 36
474
votes
14 answers

How many levels of pointers can we have?

How many pointers (*) are allowed in a single variable? Let's consider the following example. int a = 10; int *p = &a; Similarly we can have int **q = &p; int ***r = &q; and so on. For example, int ****************zz;
Parag
  • 7,746
  • 9
  • 24
  • 29
468
votes
28 answers

What are the barriers to understanding pointers and what can be done to overcome them?

Why are pointers such a leading factor of confusion for many new, and even old, college level students in C or C++? Are there any tools or thought processes that helped you understand how pointers work at the variable, function, and beyond…
David McGraw
  • 5,157
  • 7
  • 36
  • 36
457
votes
17 answers

When to use references vs. pointers

I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an API? Naturally some situations need one or the other (operator++ needs a…
connec
  • 7,231
  • 3
  • 23
  • 26
452
votes
17 answers

Why use pointers?

I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages. Basically I have three questions: Why use pointers over normal variables? When and where should I…
Anonymous
442
votes
19 answers

Passing a 2D array to a C++ function

I have a function which I want to take, as a parameter, a 2D array of variable size. So far I have this: void myFunction(double** myArray){ myArray[x][y] = 5; etc... } And I have declared an array elsewhere in my code: double…
RogerDarwin
  • 4,423
  • 3
  • 14
  • 4
401
votes
10 answers

Pointers in C: when to use the ampersand and the asterisk?

I'm just starting out with pointers, and I'm slightly confused. I know & means the address of a variable and that * can be used in front of a pointer variable to get the value of the object that is pointed to by the pointer. But things work…
Pieter
  • 31,619
  • 76
  • 167
  • 242
394
votes
14 answers

Deleting Objects in JavaScript

I'm a bit confused with JavaScript's delete operator. Take the following piece of code: var obj = { helloText: "Hello World!" }; var foo = obj; delete obj; After this piece of code has been executed, obj is null, but foo still refers to an…
393
votes
4 answers

X does not implement Y (... method has a pointer receiver)

There are already several Q&As on this "X does not implement Y (... method has a pointer receiver)" thing, but to me, they seems to be talking about different things, and not applying to my specific case. So, instead of making the question very…
xpt
  • 20,363
  • 37
  • 127
  • 216
392
votes
17 answers

How to find the size of an array (from a pointer pointing to the first element array)?

First off, here is some code: int main() { int days[] = {1,2,3,4,5}; int *ptr = days; printf("%u\n", sizeof(days)); printf("%u\n", sizeof(ptr)); return 0; } Is there a way to find out the size of the array that ptr is pointing…
jkidv
  • 4,479
  • 4
  • 23
  • 12
380
votes
11 answers

What is the difference between NULL, '\0' and 0?

In C, there appear to be differences between various values of zero -- NULL, NUL and 0. I know that the ASCII character '0' evaluates to 48 or 0x30. The NULL pointer is usually defined as: #define NULL 0 Or #define NULL (void *)0 In addition,…
gnavi
  • 25,122
  • 7
  • 21
  • 11
365
votes
19 answers

What is the difference between char * const and const char *?

What's the difference between: char * const and const char *
LB.
  • 13,730
  • 24
  • 67
  • 102
350
votes
12 answers

When should I use the new keyword in C++?

I've been using C++ for a short while, and I've been wondering about the new keyword. Simply, should I be using it, or not? With the new keyword... MyClass* myClass = new MyClass(); myClass->MyField = "Hello world!"; Without the new…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
350
votes
18 answers

Why use double indirection? or Why use pointers to pointers?

When should a double indirection be used in C? Can anyone explain with a example? What I know is that a double indirection is a pointer to a pointer. Why would I need a pointer to a pointer?
manju
  • 3,511
  • 3
  • 15
  • 4
345
votes
9 answers

Is it safe to delete a NULL pointer?

Is it safe to delete a NULL pointer? And is it a good coding style?
qiuxiafei
  • 5,827
  • 5
  • 30
  • 43