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

Reason to Pass a Pointer by Reference in C++?

Under which circumstances would you want to use code of this nature in c++? void foo(type *&in) {...} void fii() { type *choochoo; ... foo(choochoo); }
Matthew Hoggan
  • 7,402
  • 16
  • 75
  • 140
117
votes
5 answers

In Go HTTP handlers, why is the ResponseWriter a value but the Request a pointer?

I'm learning Go by writing an app for GAE, and this is signature of a handler function: func handle(w http.ResponseWriter, r *http.Request) {} I'm pointer newbie here, so why is the Request object a pointer, but the ResponseWriter not? Is there any…
Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90
114
votes
6 answers

Deleting a pointer in C++

Context: I'm trying to wrap my head around pointers, we just saw them a couple of weeks ago in school and while practicing today I ran into a silly? issue, it can be super straightforward to you but I have little to none programming experience. I've…
leopic
  • 2,958
  • 2
  • 27
  • 42
109
votes
2 answers

Return pointer to local struct

I see some code samples with constructs like this: type point struct { x, y int } func newPoint() *point { return &point{10, 20} } I have C++ background and it seems like error for me. What are the semantic of such construct? Is new point…
demi
  • 5,384
  • 6
  • 37
  • 57
108
votes
6 answers

What makes this usage of pointers unpredictable?

I'm currently learning pointers and my professor provided this piece of code as an example: //We cannot predict the behavior of this program! #include using namespace std; int main() { char * s = "My String"; char s2[] = {'a',…
trungnt
  • 1,111
  • 1
  • 7
  • 19
107
votes
6 answers

C# Store functions in a Dictionary

How do I create a Dictionary where I can store functions? Thanks. I have about 30+ functions which can be executed from the user. I want to be able to execute the function this way: private void functionName(arg1, arg2, arg3) { // code …
chi
  • 1,073
  • 2
  • 8
  • 5
106
votes
5 answers

Difference between function arguments declared with & and * in C++

I typed the following example: #include double f(double* x, double* y) { std::cout << "val x: " << *x << "\n"; std::cout << "val y: " << *y << "\n"; return *x * *y; } double f2(double &x, double &y) { std::cout << "val x:…
tim
  • 9,896
  • 20
  • 81
  • 137
104
votes
7 answers

How to compare pointers?

Suppose I have 2 pointers: int *a = something; int *b = something; If I want to compare them and see if they point at the same place does (a == b) work?
Joey Franklin
  • 6,423
  • 7
  • 25
  • 22
104
votes
4 answers

Is it good practice to free a NULL pointer in C?

Possible Duplicate: Does free(ptr) where ptr is NULL corrupt memory? I'm writing a C function that frees a pointer if it was malloc()ed. The pointer can either be NULL (in the case that an error occured and the code didn't get the chance to…
rid
  • 61,078
  • 31
  • 152
  • 193
104
votes
5 answers

Understanding and relationship between Box, ref, & and *

I'm a bit confused about how pointers work in Rust. There's ref, Box, &, *, and I'm not sure how they work together. Here's how I understand it currently: Box isn't really a pointer - it's a way to allocate data on the heap, and pass around unsized…
zrneely
  • 1,802
  • 3
  • 17
  • 26
104
votes
18 answers

What is the point of function pointers?

I have trouble seeing the utility of function pointers. I guess it may be useful in some cases (they exist, after all), but I can't think of a case where it's better or unavoidable to use a function pointer. Could you give some example of good use…
gramm
  • 18,786
  • 7
  • 27
  • 27
103
votes
30 answers

Testing pointers for validity (C/C++)

Is there any way to determine (programatically, of course) if a given pointer is "valid"? Checking for NULL is easy, but what about things like 0x00001234? When trying to dereference this kind of pointer an exception/crash occurs. A cross-platform…
noamtm
  • 12,435
  • 15
  • 71
  • 107
101
votes
3 answers

What is an opaque pointer in C?

May I know the usage and logic behind the opaque pointer concept in C?
Renjith G
  • 4,718
  • 14
  • 42
  • 56
101
votes
5 answers

Correct way of declaring pointer variables in C/C++

I noticed some people use the following notation for declaring pointer variables. (a) char* p; instead of (b) char *p; I use (b). What is the rational behind the notation (a)? Notation (b) makes more sense to me because character pointer is not a…
keeda
  • 2,605
  • 5
  • 28
  • 27
100
votes
4 answers

Can a pointer to base point to an array of derived objects?

I went to a job interview today and was given this interesting question. Besides the memory leak and the fact there is no virtual dtor, why does this code crash? #include //besides the obvious mem leak, why does this code crash? class…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415