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
74
votes
10 answers

Why do some people prefer "T const&" over "const T&"?

So, I realize that const T& and T const& are identical and both mean a reference to a const T. In both cases, the reference is also constant (references cannot be reassigned, unlike pointers). I've observed, in my somewhat limited experience, that…
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
74
votes
14 answers

Is it possible to store the address of a label in a variable and use goto to jump to it?

I know everyone hates gotos. In my code, for reasons I have considered and am comfortable with, they provide an effective solution (ie I'm not looking for "don't do that" as an answer, I understand your reservations, and understand why I am using…
Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
73
votes
7 answers

How can I check if char* variable points to empty string?

How can I check if char* variable points to an empty string?
Aan
  • 12,247
  • 36
  • 89
  • 150
73
votes
9 answers

Returning a pointer to a vector element in c++

I have a vector of myObjects in global scope. I have a method which uses a std::vector::const_iterator to traverse the vector, and doing some comparisons to find a specific element. Once I have found the required element, I want to be able…
Krakkos
  • 1,439
  • 3
  • 19
  • 24
73
votes
8 answers

What is a dangling pointer?

I know this is pretty common question, but still new for me! I don't understand concept of dangling pointer, was googling around, and writing test methods to find one. I just wonder is this a dangling pointer? As whatever example I found was…
code muncher
  • 1,592
  • 2
  • 27
  • 46
72
votes
9 answers

Length of array in function argument

This is well known code to compute array length in C: sizeof(array)/sizeof(type) But I can't seem to find out the length of the array passed as an argument to a function: #include int length(const char* array[]) { return…
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
72
votes
8 answers

difference between a pointer and reference parameter?

Are these the same: int foo(bar* p) { return p->someInt(); } and int foo(bar& r) { return r.someInt(); } Ignore the null pointer potential. Are these two functions functionally identical no matter if someInt() is virtual or if they are passed…
criddell
  • 14,289
  • 9
  • 41
  • 45
72
votes
8 answers

Does Function pointer make the program slow?

I read about function pointers in C. And everyone said that will make my program run slow. Is it true? I made a program to check it. And I got the same results on both cases. (measure the time.) So, is it bad to use function pointer? Thanks in…
drigoSkalWalker
  • 2,742
  • 9
  • 32
  • 45
72
votes
6 answers

Pointer values are different but they compare equal. Why?

A short example outputs a weird result! #include using namespace std; struct A { int a; }; struct B { int b; }; struct C : A, B { int c; }; int main() { C* c = new C; B* b = c; cout << "The address of b is 0x" <<…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
71
votes
7 answers

Are there any platforms where pointers to different types have different sizes?

The C standard allows pointers to different types to have different sizes, e.g. sizeof(char*) != sizeof(int*) is permitted. It does, however, require that if a pointer is converted to a void* and then converted back to its original type, it must…
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
71
votes
3 answers

Is it legal to compare dangling pointers?

Is it legal to compare dangling pointers? int *p, *q; { int a; p = &a; } { int b; q = &b; } std::cout << (p == q) << '\n'; Note how both p and q point to objects that have already vanished. Is this legal?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
71
votes
16 answers

How can I get the size of an array from a pointer in C?

I've allocated an "array" of mystruct of size n like this: if (NULL == (p = calloc(sizeof(struct mystruct) * n,1))) { /* handle error */ } Later on, I only have access to p, and no longer have n. Is there a way to determine the length of the array…
Joel
  • 11,431
  • 17
  • 62
  • 72
70
votes
13 answers

Post-increment on a dereferenced pointer?

Trying to understand the behaviour of pointers in C, I was a little surprised by the following (example code below): #include void add_one_v1(int *our_var_ptr) { *our_var_ptr = *our_var_ptr +1; } void add_one_v2(int *our_var_ptr) { …
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
69
votes
6 answers

What is the difference between returning a char* and a char[] from a function?

Why does the first function return the string "Hello, World" but the second function returns nothing. I thought the return value of both of the functions would be undefined since they are returning data that is out of scope. #include //…
Tobs
  • 493
  • 4
  • 6
69
votes
2 answers

Why does printing a pointer print the same thing as printing the dereferenced pointer?

From the Rust guide: To dereference (get the value being referred to rather than the reference itself) y, we use the asterisk (*) So I did it: fn main() { let x = 1; let ptr_y = &x; println!("x: {}, ptr_y: {}", x, *ptr_y); } This…
Vega
  • 2,661
  • 5
  • 24
  • 49