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
342
votes
4 answers

Differences between unique_ptr and shared_ptr

Possible Duplicates: pimpl: shared_ptr or unique_ptr smart pointers (boost) explained Could someone explain differences between shared_ptr and unique_ptr?
smallB
  • 16,662
  • 33
  • 107
  • 151
337
votes
5 answers

What is the uintptr_t data type?

What is uintptr_t and what can it be used for?
dimba
  • 26,717
  • 34
  • 141
  • 196
333
votes
12 answers

Arrow operator (->) usage in C

I am reading a book called "Teach Yourself C in 21 Days" (I have already learned Java and C# so I am moving at a much faster pace). I was reading the chapter on pointers and the -> (arrow) operator came up without explanation. I think that it is…
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
330
votes
4 answers

NULL vs nullptr (Why was it replaced?)

I know that in C++ 0x or NULL was replaced by nullptr in pointer-based applications. I'm just curious of the exact reason why they made this replacement? In what scenario is using nullptr over NULL beneficial when dealing with pointers?
Riptyde4
  • 5,134
  • 8
  • 30
  • 57
330
votes
4 answers

Why does the arrow (->) operator in C exist?

The dot (.) operator is used to access a member of a struct, while the arrow operator (->) in C is used to access a member of a struct which is referenced by the pointer in question. The pointer itself does not have any members which could be…
Askaga
  • 6,061
  • 5
  • 25
  • 49
314
votes
19 answers

Pointer to class data member "::*"

I came across this strange code snippet which compiles fine: class Car { public: int speed; }; int main() { int Car::*pSpeed = &Car::speed; return 0; } Why does C++ have this pointer to a non-static data member of a class? What is…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
292
votes
21 answers

Return array in a function

I have an array int arr[5] that is passed to a function fillarr(int arr[]): int fillarr(int arr[]) { for(...); return arr; } How can I return that array? How will I use it, say I returned a pointer how am I going to access it?
Ismail Marmoush
  • 13,140
  • 25
  • 80
  • 114
280
votes
7 answers

Are there benefits of passing by pointer over passing by reference in C++?

What are the benefits of passing by pointer over passing by reference in C++? Lately, I have seen a number of examples that chose passing function arguments by pointers instead of passing by reference. Are there benefits to doing…
Matt Pascoe
  • 8,651
  • 17
  • 42
  • 48
277
votes
8 answers

What is the difference between char array and char pointer in C?

I am trying to understand pointers in C but I am currently confused with the following: char *p = "hello" This is a char pointer pointing at the character array, starting at h. char p[] = "hello" This is an array that stores hello. What is the…
diesel
  • 3,337
  • 4
  • 20
  • 16
276
votes
12 answers

Pointer vs. Reference

What would be better practice when giving a function the original variable to work with: unsigned long x = 4; void func1(unsigned long& val) { val = 5; } func1(x); or: void func2(unsigned long* val) { *val =…
Jack Reza
  • 2,771
  • 3
  • 16
  • 4
274
votes
8 answers

How to pass objects to functions in C++?

I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++. Do I need to pass pointers, references, or non-pointer and non-reference values? I remember in Java there are no such issues…
Rakesh K
  • 8,237
  • 18
  • 51
  • 64
270
votes
8 answers

size_t vs. uintptr_t

The C standard guarantees that size_t is a type that can hold any array index. This means that, logically, size_t should be able to hold any pointer type. I've read on some sites that I found on the Googles that this is legal and/or should always…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
266
votes
9 answers

Difference between char* and const char*?

What's the difference between char* name which points to a constant string literal, and const char* name
Iceman
  • 4,202
  • 7
  • 26
  • 39
265
votes
8 answers

Constant pointer vs Pointer to constant

I want to know the difference between const int* ptr; and int * const ptr; and how it works. It is pretty difficult for me to understand or keep remember this. Please help.
Venkatesh K
  • 4,364
  • 4
  • 18
  • 26
263
votes
9 answers

Is an array name a pointer?

Is an array's name a pointer in C? If not, what is the difference between an array's name and a pointer variable?
user188276