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

Objective-C pointers?

I am new to coding and trying to get up to speed with Objective-C. Came across some code I did not understand. I was hoping someone could clarify it for me. In the case below, I am not sure how *foo2 is working and why it is not being released?…
archieoi
  • 429
  • 1
  • 6
  • 9
12
votes
3 answers

Calculate array length via pointer arithmetic

I was wondering how *(&array + 1) actually works. I saw this as an easy way to calculate the array length and want to understand it properly before using it. I'm not very experienced with pointer arithmetic, but with my understanding &array gives…
Varrick
  • 153
  • 8
12
votes
5 answers

More linked lists in C

Before I begin I want to make it clear that I don't want the answer to my HOMEWORK problem, I would just like if someone could actually explain what exactly my instructor is asking for in this assignment (preferably a dumbed down version) and maybe…
none
  • 263
  • 6
  • 13
12
votes
7 answers

Pointer to an Integer in Java

I have a code like this int a,b; switch(whatever){ case 1: lots_of_lines_dealing_with_variable_a; case 2: same_lines_but_dealing_with_variable_b; } I thought of doing: int a,b; pointer_to_int p; switch(whatever){ case 1: …
bluehallu
  • 10,205
  • 9
  • 44
  • 61
12
votes
5 answers

C++ Class Copy (pointer copy)

It is my understanding that when you make a copy of a class that defines a pointer variable, the pointer is copied, but the data that the pointer is pointing to is not. My question is: Is one to assume that the "pointer copy" in this case is simply…
Chad Kemp
  • 139
  • 1
  • 1
  • 9
12
votes
4 answers

char ** and dereferencing pointers

I would like to end my confusion with the char ** When once creates an array of character arrays(strings) how does char ** actually accomplish this? i get that char * is a pointer to a char and that char *array[] is an array of char pointers, but…
some_id
  • 29,466
  • 62
  • 182
  • 304
12
votes
3 answers

C++ multiple unique pointers from same raw pointer

Consider my code below. My understanding of unique pointers was that only one unique pointer can be used to reference one variable or object. In my code I have more than one unique_ptr accessing the same variable. It's obviously not the correct way…
Engineer999
  • 3,683
  • 6
  • 33
  • 71
12
votes
2 answers

Can't pass 'const pointer const' to const ref

Suppose you have a set of pointers (yeah...) : std::set myTypeContainer; Then suppose that you want to search this set from a const method of SomeType: bool SomeType::IsContainered() const { return myTypeContainer.find(this) !=…
12
votes
4 answers

C/C++ structure incomplete member type inconsistency?

Lets say we define two structures in either C or C++ (I get the same result in both C and C++ and I think the rules are the same, tell me if they are not). one with a value member of an incomplete type: struct abc{ int data; struct nonexsitnow…
alandawkins
  • 309
  • 3
  • 10
12
votes
6 answers

How do you check for an invalid pointer?

My current code to the effect of: if( objectPointer != NULL){ delete objectPointer; } doesn't work because the pointers are getting set to invalid hex numbers by the compiler such as: 0xbaadf00d 0xdeadbeef etc.... So what's the best way to…
DShook
  • 14,833
  • 9
  • 45
  • 55
12
votes
2 answers

Addresses of slices of empty structs

I have a basic question on empty structs and am trying to get my head around the following different outputs I am getting when attempting to get the address of backing arrays' elements for two slices: a := make([]struct{}, 10) b := make([]struct{},…
Kevin Ghaboosi
  • 606
  • 10
  • 20
12
votes
7 answers

(nil) pointer in C/C++

I am working on a project and I keep coming across this error that will not allow me to complete the project. When I initialize one of my pointers to point to an object that will be made during the execution of the program and I initialize it to…
CF711
  • 301
  • 1
  • 5
  • 14
12
votes
9 answers

Why is a c++ reference considered safer than a pointer?

When the c++ compiler generates very similar assembler code for a reference and pointer, why is using references preferred (and considered safer) compared to pointers? I did see Difference between pointer variable and reference variable in C++…
yasouser
  • 5,113
  • 2
  • 27
  • 41
12
votes
3 answers

How to Cast Integer Value to Pointer Address Without Triggering Warnings

I have the following variable uint32_t Value = 0x80 0x80 represents an address in the memory e.g. // Write 2 at address 0x80 *(uint32_t*)((uint32_t)0x80) = 2; How can i cast Value to a Pointer, so it points to 0x80? uint32_t *Pointer = ??…
Progga
  • 343
  • 1
  • 3
  • 11
12
votes
6 answers

ptr->hello(); /* VERSUS */ (*ptr).hello();

I was learning about C++ pointers and the -> operator seemed strange to me. Instead of ptr->hello(); one could write (*ptr).hello(); because it also seems to work, so I thought the former is just a more convenient way. Is that the case or is there…
Joey
  • 444
  • 2
  • 7