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
66
votes
1 answer

When to use pointers

I'm new to the Go Language, and have only minimal background in C/C++, so naturally I'm struggling with the idea of when to use pointers and when not to use pointers. Although this question might be considered open-ended, I'm wondering what some…
Jim Pedid
  • 2,730
  • 3
  • 23
  • 27
66
votes
8 answers

Are pointers considered a method of calling by reference in C?

In my University's C programming class, the professor and subsequent book written by her uses the term call or pass by reference when referring to pointers in C. An example of what is considered a 'call by reference function' by my professor: int…
Insane
  • 889
  • 10
  • 21
66
votes
4 answers

How to get the pointer of return value from function call?

I just need a pointer to time.Time, so the code below seems invalid: ./c.go:5: cannot take the address of time.Now() I just wonder why? Is there any way to do that except to do assignment to a variable first and take the pointer of the…
Meng
  • 747
  • 1
  • 6
  • 5
66
votes
6 answers

Difference between pointer to a reference and reference to a pointer

What is the difference between pointer to a reference, reference to a pointer and pointer to a pointer in C++? Where should one be preferred over the other?
user220751
66
votes
11 answers

What happens to memory after '\0' in a C string?

Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the user of my function a C-string, whose length I do not know at the beginning of the function. I can place only an upper bound on the length at the outset,…
Erika Electra
  • 1,854
  • 3
  • 20
  • 31
65
votes
7 answers

Pointer Arithmetic

Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure the audience is a bunch of Java programmers learning C and C++.
leora
  • 188,729
  • 360
  • 878
  • 1,366
65
votes
5 answers

How to set bool pointer to true in struct literal?

I have the function below which accepts a bool pointer. I'm wondering if there is any notation which allows me to set the value of the is field to true in the struct literal; basically without to define a new identifier (i.e. var x := true ;…
The user with no hat
  • 10,166
  • 20
  • 57
  • 80
65
votes
8 answers

C++ Array of pointers: delete or delete []?

Cosider the following code: class Foo { Monster* monsters[6]; Foo() { for (int i = 0; i < 6; i++) { monsters[i] = new Monster(); } } virtual ~Foo(); } What is the correct…
Jasper
  • 11,590
  • 6
  • 38
  • 55
65
votes
5 answers

Changing address contained by pointer using function

If I've declared a pointer p as int *p; in main module, I can change the address contained by p by assigning p = &a; where a is another integer variable already declared. I now want to change the address by using a function as: void…
pranphy
  • 1,787
  • 4
  • 16
  • 22
65
votes
8 answers

Using pointers to remove item from singly-linked list

In a recent Slashdot Interview Linus Torvalds gave an example of how some people use pointers in a way that indicates they don't really understand how to use them correctly. Unfortunately, since I'm one of the people he's talking about, I also…
codebox
  • 19,927
  • 9
  • 63
  • 81
64
votes
6 answers

Why is the dereference operator (*) also used to declare a pointer?

I'm not sure if this is a proper programming question, but it's something that has always bothered me, and I wonder if I'm the only one. When initially learning C++, I understood the concept of references, but pointers had me confused. Why, you ask?…
diggingforfire
  • 3,359
  • 1
  • 23
  • 33
64
votes
8 answers

Declaring array of int

Is there any difference between these two declarations? int x[10]; vs. int* x = new int[10]; I suppose the former declaration (like the latter one) is a pointer declaration and both variables could be treated the same. Does it mean they are…
B Faley
  • 17,120
  • 43
  • 133
  • 223
64
votes
6 answers

C++ Returning Pointers/References

I have a fairly good understanding of the dereferencing operator, the address of operator, and pointers in general. I however get confused when I see stuff such as this: int* returnA() { int *j = &a; return j; } int* returnB() { return…
anon235370
63
votes
5 answers

What does "int* p=+s;" do?

I saw a weird type of program here. int main() { int s[]={3,6,9,12,18}; int* p=+s; } Above program tested on GCC and Clang compilers and working fine on both compilers. I curious to know, What does int* p=+s; do? Is array s decayed to…
msc
  • 33,420
  • 29
  • 119
  • 214
63
votes
6 answers

In C, how would I choose whether to return a struct or a pointer to a struct?

Working on my C muscle lately and looking through the many libraries I've been working with its certainly gave me a good idea of what is good practice. One thing that I have NOT seen is a function that returns a struct: something_t make_something()…
Dellowar
  • 3,160
  • 1
  • 18
  • 37