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
78
votes
6 answers

Pointer to incomplete class type is not allowed

For some reason I cannot use functions attached to the object I want to use. I added a comment to the line that is not working. As an error I get "Error; pointer to incomplete class type is not allowed" Please help This is code in dokter.cpp int…
Sharpless512
  • 3,062
  • 5
  • 35
  • 60
77
votes
15 answers

What is the reason for using a double pointer when adding a node in a linked list?

The two code examples below both add a node at the top of a linked list. But whereas the first code example uses a double pointer the second code example uses a single pointer code example 1: struct node* push(struct node **head, int data) { …
a6h
  • 773
  • 2
  • 6
  • 5
77
votes
15 answers

When is an integer<->pointer cast actually correct?

The common folklore says that: The type system exists for a reason. Integers and pointers are distinct types, casting between them is a malpractice in the majority of cases, may indicate a design error and should be avoided. Even when such a cast…
Kos
  • 70,399
  • 25
  • 169
  • 233
77
votes
16 answers

How to get the real and total length of char * (char array)?

For a char [], I can easily get its length by: char a[] = "aaaaa"; int length = sizeof(a)/sizeof(char); // length=6 However, I cannot do like this to get the length of a char * by: char *a = new char[10]; int length =…
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
77
votes
4 answers

How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++?

I'm using a vector of pointers to objects. These objects are derived from a base class, and are being dynamically allocated and stored. For example, I have something like: vector Enemies; and I'll be deriving from the Enemy class and then…
akif
  • 12,034
  • 24
  • 73
  • 85
77
votes
17 answers

Duplicating objects in Java

I learned that when you modify a variable in Java it doesn't change the variable it was based on int a = new Integer(5); int b = a; b = b + b; System.out.println(a); // 5 as expected System.out.println(b); // 10 as expected I assumed a similar…
user1581900
  • 3,680
  • 4
  • 18
  • 21
76
votes
5 answers

Why can a string be assigned to a char* pointer, but not to a char[] array?

Can someone explain why this works with the pointer: char * str1; str1 = "Hello1"; str1 = "new string"; // but not this char str2 [] = "hello"; str2 = "four"; // or this char str3 []; str3 = "hello"; str3 = "hello";
nick
  • 1,147
  • 1
  • 11
  • 12
76
votes
2 answers

gsl::not_null vs. std::reference_wrapper vs. T&

C++ Core Guidelines has been presented recently (congrats!) and I am concerned about gsl::not_null type. As stated in I.12: Declare a pointer that must not be null as not_null: To help avoid dereferencing nullptr errors. To improve performance by …
Mikhail
  • 20,685
  • 7
  • 70
  • 146
76
votes
3 answers

How to use an iterator?

I'm trying to calculate the distance between two points. The two points I stored in a vector in C++: (0,0) and (1,1). I'm supposed to get results as 0 1.4 1.4 0 But the actual result that I got is 0 1 -1 0 I think there's something wrong with the…
user188276
76
votes
3 answers

C++ inserting unique_ptr in map

I have a C++ object of type ObjectArray typedef map> ObjectArray; What is the syntax to create a unique_ptr to a new object of type Class1 and insert it into an object of type ObjectArray?
vigs1990
  • 1,639
  • 4
  • 17
  • 19
76
votes
23 answers

Why not use pointers for everything in C++?

Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int x; int y; } Then write some code using it. Why would I do the following? Pixel p; p.x = 2; p.y = 5; Coming from a Java world I always…
Eric
  • 19,525
  • 19
  • 84
  • 147
75
votes
3 answers

Pointer to a map

Having some maps defined as: var valueToSomeType = map[uint8]someType{...} var nameToSomeType = map[string]someType{...} I would want a variable that points to the address of the maps (to don't copy all variable). I tried it using: valueTo :=…
user316368
  • 1,135
  • 2
  • 9
  • 12
74
votes
5 answers

Is NULL always zero in C?

I was interviewing a guy for a mid-level software engineering position yesterday, and he mentioned that in C, NULL is not always zero and that he had seen implementations of C where NULL is not zero. I find this highly suspect, but I want to be…
chi42
  • 925
  • 1
  • 7
  • 8
74
votes
7 answers

How do I modify a pointer that has been passed into a function in C?

So, I have some code, kind of like the following, to add a struct to a list of structs: void barPush(BarList * list,Bar * bar) { // if there is no move to add, then we are done if (bar == NULL) return;//EMPTY_LIST; // allocate space for…
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
74
votes
12 answers

Pointers to pointers vs. normal pointers

The purpose of a pointer is to save the address of a specific variable. Then the memory structure of following code should look like: int a = 5; int *b = &a; ...... memory address ...... value a ... 0x000002 ................... 5 b…
user42298
  • 1,831
  • 3
  • 19
  • 30