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
87
votes
3 answers

Does 'auto' type assignments of a pointer in c++11 require '*'?

Given my variable being a pointer, if I assign it to a variable of "auto" type, do I specify the "*" ? std::vector *getVector(); //returns populated vector //... std::vector *myvector = getVector(); //assume has n items in…
Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100
86
votes
5 answers

How is "int* ptr = int()" value initialization not illegal?

The following code (taken from here): int* ptr = int(); compiles in Visual C++ and value-initializes the pointer. How is that possible? I mean int() yields an object of type int and I can't assign an int to a pointer. How is the code above not…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
85
votes
8 answers

What is the size of a pointer?

Is the size of a pointer the same as the size as the type it's pointing to, or do pointers always have a fixed size? For example... int x = 10; int * xPtr = &x; char y = 'a'; char * yPtr = &y; std::cout << sizeof(x) << "\n"; std::cout <<…
MGZero
  • 5,812
  • 5
  • 29
  • 46
85
votes
3 answers

Is a pointer with the right address and type still always a valid pointer since C++17?

(In reference to this question and answer.) Before the C++17 standard, the following sentence was included in [basic.compound]/3: If an object of type T is located at an address A, a pointer of type cv T* whose value is the address A is said to…
Oliv
  • 17,610
  • 1
  • 29
  • 72
85
votes
6 answers

Why doesn't polymorphism work without pointers/references?

I did find some questions already on StackOverflow with similar title, but when I read the answers, they were focusing on different parts of the question, which were really specific (e.g. STL/containers). Could someone please show me, why you must…
user997112
  • 29,025
  • 43
  • 182
  • 361
84
votes
5 answers

What are the rules for casting pointers in C?

K&R doesn't go over it, but they use it. I tried seeing how it'd work by writing an example program, but it didn't go so well: #include int bleh (int *); int main(){ char c = '5'; char *d = &c; bleh((int *)d); return…
Theo Chronic
  • 1,623
  • 2
  • 15
  • 16
84
votes
6 answers

what's the point of std::unique_ptr::get

Doesn't std::unique_ptr::get defeat the purpose of having a unique_ptr in the first place? I would have expected this function to change its state so it holds no more pointer. Is there an actual useful use of std::unique_ptr::get?
lezebulon
  • 7,607
  • 11
  • 42
  • 73
83
votes
4 answers

What is the function of an asterisk before a function name?

I've been confused with what I see on most C programs that has unfamiliar function declaration for me. void *func_name(void *param){ ... } What does * imply for the function? My understanding about (*) in a variable type is that it creates a…
Aldee
  • 4,439
  • 10
  • 48
  • 71
83
votes
2 answers

increment value of int being pointed to by pointer

I have an int pointer (i.e., int *count) that I want to increment the integer being pointed at by using the ++ operator. I thought I would call: *count++; However, I am getting a build warning "expression result unused". I can: call *count +=…
joels
  • 7,249
  • 11
  • 53
  • 94
83
votes
9 answers

C Programming: malloc() inside another function

I need help with malloc() inside another function. I'm passing a pointer and size to the function from my main() and I would like to allocate memory for that pointer dynamically using malloc() from inside that called function, but what I see is…
HaggarTheHorrible
  • 7,083
  • 20
  • 70
  • 81
83
votes
5 answers

How does dereferencing of a function pointer happen?

Why and how does dereferencing a function pointer just "do nothing"? This is what I am talking about: #include void hello() { printf("hello"); } int main(void) { (*****hello)(); } From a comment over here: function pointers…
Lazer
  • 90,700
  • 113
  • 281
  • 364
82
votes
6 answers

const char* and char const* - are they the same?

From my understanding, const modifiers should be read from right to left. From that, I get that: const char* is a pointer whose char elements can't be modified, but the pointer itself can, and char const* is a constant pointer to mutable…
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
82
votes
7 answers

C++: difference between ampersand "&" and asterisk "*" in function/method declaration?

Is there some kind of subtle difference between those: void a1(float &b) { b=1; }; a1(b); and void a1(float *b) { (*b)=1; }; a1(&b); ? They both do the same (or so it seems from main() ), but the first one is obviously shorter, however…
Slava V
  • 16,686
  • 14
  • 60
  • 63
82
votes
5 answers

"int *nums = {5, 2, 1, 4}" causes a segmentation fault

int *nums = {5, 2, 1, 4}; printf("%d\n", nums[0]); causes a segfault, whereas int nums[] = {5, 2, 1, 4}; printf("%d\n", nums[0]); doesn't. Now: int *nums = {5, 2, 1, 4}; printf("%d\n", nums); prints 5. Based on this, I have conjectured that the…
user1299784
  • 2,019
  • 18
  • 30
82
votes
6 answers

Print the address or pointer for value in C

I want to do something that seems fairly simple. I get results but the problem is, I have no way to know if the results are correct. I'm working in C and I have two pointers; I want to print the contents of the pointer. I don't want to dereference…
Frank V
  • 25,141
  • 34
  • 106
  • 144