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

Proper way of casting pointer types

Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast? I used…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
68
votes
11 answers

What's the difference between a null pointer and a void pointer?

Whats the difference between a Null pointer & a Void pointer?
Pavitar
  • 4,282
  • 10
  • 50
  • 82
68
votes
7 answers

Dynamically allocating an array of objects

I have a class that contains a dynamically allocated array, say class A { int* myArray; A() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB's…
Domenic
  • 110,262
  • 41
  • 219
  • 271
68
votes
5 answers

initial value of reference to non-const must be an lvalue

I'm trying to send value into function using reference pointer but it gave me a completely non-obvious error #include "stdafx.h" #include using namespace std; void test(float *&x){ *x = 1000; } int main(){ float nKByte =…
Mohd Shahril
  • 2,257
  • 5
  • 25
  • 30
68
votes
3 answers

Free memory allocated in a different function?

I'm trying to learn C and I'm currently trying to write a basic stack data structure, but I can't seem to get basic malloc/free right. Here's the code I've been using (I'm just posting a small part here to illustrate a specific problem, not the…
Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
67
votes
5 answers

Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer

This question is meant to be used as reference for all frequently asked questions of the nature: Why do I get a mysterious crash or "segmentation fault" when I copy/scan data to the address where an uninitialised pointer points to? For…
Lundin
  • 195,001
  • 40
  • 254
  • 396
67
votes
2 answers

How to printf a memory address in C

My code is: #include #include void main() { char string[10]; int A = -73; unsigned int B = 31337; strcpy(string, "sample"); // printing with different formats printf("[A] Dec: %d, Hex: %x,…
varlotbarnacle
  • 821
  • 1
  • 6
  • 7
67
votes
14 answers

Efficiency: arrays vs pointers

Memory access through pointers is said to be more efficient than memory access through an array. I am learning C and the above is stated in K&R. Specifically they say Any operation that can be achieved by array subscripting can also be done with…
Abhijith Madhav
  • 2,748
  • 5
  • 33
  • 44
67
votes
2 answers

cpp / c++ get pointer value or depointerize pointer

I was wondering if it's possible to make a pointer not a pointer.. The problem is I have a function that accepts a pointer for an paramater for me to easily get a value to that pointer. It's a simple int so I was wondering if I could just get that…
Rasmus
  • 1,605
  • 3
  • 16
  • 20
66
votes
5 answers

Dot (".") operator and arrow ("->") operator use in C vs. Objective-C

I'm trying to wrap my head around some of the differences in usage and syntax in C vs. Objective-C. In particular, I want to know how (and why) the usage differs for the dot operator and the arrow operator in C vs. Objective-C. Here is a simple…
Charles
  • 865
  • 1
  • 8
  • 9
66
votes
5 answers

Javascript pointer/reference craziness. Can someone explain this?

Javascript passes objects by reference. This makes perfect sense. But once you start manipulating those objects, everything acts in a way that seem unintuitive. Let me offer an example: var a, b; a = {} b = a; a['one'] = {}; console.log(…
Philip Walton
  • 29,693
  • 16
  • 60
  • 84
66
votes
13 answers

Convert iterator to pointer?

I have a std::vector with n elements. Now I need to pass a pointer to a vector that has the last n-1 elements to a function. For example, my vector foo contains (5,2,6,87,251). A function takes vector* and I want to pass it a pointer to…
Frank
  • 64,140
  • 93
  • 237
  • 324
66
votes
10 answers

How to access a local variable from a different function using pointers?

May I have any access to a local variable in a different function? If so, how? void replaceNumberAndPrint(int array[3]) { printf("%i\n", array[1]); printf("%i\n", array[1]); } int * getArray() { int myArray[3] = {4, 65, 23}; return…
Radek Simko
  • 15,886
  • 17
  • 69
  • 107
66
votes
5 answers

How to assign pointer address manually in C programming language?

How do you assign a pointer address manually (e.g. to memory address 0x28ff44) in the C programming language?
Irakli
  • 901
  • 1
  • 7
  • 12
66
votes
5 answers

In C, what does a variable declaration with two asterisks (**) mean?

I am working with C and I'm a bit rusty. I am aware that * has three uses: Declaring a pointer. Dereferencing a pointer. Multiplication However, what does it mean when there are two asterisks (**) before a variable declaration: char **aPointer =…
Scott Davies
  • 3,665
  • 6
  • 31
  • 39