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

Operator precedence in the given expressions

Expression 1: *p++; where p is a pointer to integer. p will be incremented first and then the value to which it is pointing to is taken due to associativity(right to left). Is it right? Expression 2: a=*p++; where p is a pointer to integer. Value of…
Vipul Prakash
  • 173
  • 1
  • 9
12
votes
2 answers

How to set a constexpr pointer to a physical Address

In embedded programming you often need to set pointers that point to a physical address. The address is non relocatable and fixed. These are not set by the linker as typically they represent registers or in this case calibration data located at a…
Andrew Goedhart
  • 937
  • 9
  • 17
12
votes
9 answers

C++ Objects: When should I use pointer or reference

I can use an object as pointer to it, or its reference. I understand that the difference is that pointers have to be deleted manually, and references remain until they are out of scope. When should I use each of them? What is the practical…
rafaelxy
  • 312
  • 3
  • 12
12
votes
4 answers

Initialize pointer receiver in pointer method Go

How can I initialize a pointer receiver with a pointer method? package main import "fmt" type Person struct { name string age int } func (p *Person) Born() { if nil == p { p = new(Person) } } func main() { var…
Francis
  • 121
  • 1
  • 1
  • 4
12
votes
2 answers

Unify C++ templates for pointers, values and smart pointers

My real example is quite big, so I will use a simplified one. Suppose I have a data-type for a rectangle: struct Rectangle { int width; int height; int computeArea() { return width * height; } } And another type that consumes that…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
12
votes
6 answers

difference between if(pointer) vs if(pointer != NULL) in c++, cpplint issue

I already checked this post Can I use if (pointer) instead of if (pointer != NULL)? and some other posts on net. But it is not stating any difference between two statements. Problem: As I run cpplint.py on my cpp code, I found issues where I check…
ashish
  • 319
  • 2
  • 12
12
votes
1 answer

Slicing a slice pointer passed as argument

I have the following code: func main() { var buf []byte{1, 2, 3, 4, 5} buf = buf[2:] fmt.Println(buf) panic(1) } However I want to pass a pointer to buf byte slice to another function, and slice it there, so something like: func…
user99999
  • 1,994
  • 5
  • 24
  • 45
12
votes
3 answers

converting vector iterator to pointer

I have a vector std::vector. I would like to iterate the vector for finding a match, if found would like to return the pointer to the element as below: const int * findint(std::vector &v, int a) { std::vector::const_iterator…
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
12
votes
6 answers

using an absolute pointer address as a template argument

I have a template class which takes as its first template argument a foo * pointer. I'd like to instantiate one of these with a foo located at an absolute address, like so: class foo { int baz; }; template class bar { public: bar()…
Charlie Skilbeck
  • 1,081
  • 2
  • 15
  • 38
12
votes
3 answers

Have a template parameter that can be pointer type or non-pointer type

Suppose I have something like: template void do_something(T t){ pass_it_somewhere(t); t->do_something(); } Now it would be useful that T is allowed to be a pointer- or a non-pointer type. Function do_something(...) can basically…
Michael
  • 7,407
  • 8
  • 41
  • 84
12
votes
1 answer

Immutable strings in Go

is anyone able to explain me why the address of &c1.name is the same after being changed in function changeMe(). I thought strings are immutable in Go. package main import "fmt" type customer struct { name string age int } func main() { …
camabeh
  • 1,122
  • 12
  • 13
12
votes
6 answers

C++ Linked list using smart pointers

I have only been using raw pointers for linked list with templates. For example, the member data, Node* head; and when I am inserting a node one of the lines would be head = new Node(data);. However, now I need to use a smart pointer and I am…
Mark
  • 283
  • 1
  • 2
  • 14
12
votes
2 answers

Comparing pointer with char end of string

I know that when comparing a char pointer with some char value you need to prefix the pointer with *, but I have found in some code a comparison like: char* c; // ... while (*c != ']' && *c != '\0') // search for some character { c++; } if (c…
sop
  • 3,445
  • 8
  • 41
  • 84
12
votes
5 answers

C# Pointers in a Method's arguments?

I wish to directly modify a variable's value outside of a method from inside it. Pointers are the way, correct? How?
Steffan Donal
  • 2,244
  • 4
  • 24
  • 47
12
votes
5 answers

C++ static const access through a NULL pointer

class Foo { public: static const int kType = 42; }; void Func() { Foo *bar = NULL; int x = bar->kType; putc(x, stderr); } Is this defined behavior? I read through the C++ standard but couldn't find anything about accessing a static const value…
sirg3
  • 123
  • 4