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
99
votes
5 answers

error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’

Wrong form: int &z = 12; Correct form: int y; int &r = y; Question: Why is the first code wrong? What is the "meaning" of the error in the title?
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
98
votes
9 answers

Are Python variables pointers? Or else, what are they?

Variables in Python are just pointers, as far as I know. Based on this rule, I can assume that the result for this code snippet: i = 5 j = i j = 3 print(i) would be 3. But I got an unexpected result for me, and it was 5. Moreover, my Python book…
Lior
  • 5,841
  • 9
  • 32
  • 46
97
votes
7 answers

C++ pass an array by reference

is this allowed to pass an array by reference ? void foo(double& *bar) Seems that my compiler says no. Why? What is the proper way to pass an array by reference? Or a work around? I have an array argument that my method should modify and that I…
kiriloff
  • 25,609
  • 37
  • 148
  • 229
96
votes
8 answers

Meaning of *& and **& in C++

I found these symbols in a function declaration several times, but I don't know what they mean. Example: void raccogli_dati(double **& V, double **p, int N) { int ultimo = 3; V = new double * [N/2]; for(int i=0; i < N/2; i++) { V[i] =…
sdffadsf
  • 1,132
  • 2
  • 9
  • 8
96
votes
8 answers

Is it possible to initialize a C pointer to NULL?

I had been writing things like char *x=NULL; on the assumption that char *x=2; would create a char pointer to address 2. But, in The GNU C Programming Tutorial it says that int *my_int_ptr = 2; stores the integer value 2 to whatever random…
fagricipni
  • 927
  • 1
  • 7
  • 9
95
votes
10 answers

What is the difference between a C# Reference and a Pointer?

I do not quite understand the difference between a C# reference and a pointer. They both point to a place in memory don't they? The only difference I can figure out is that pointers are not as clever, cannot point to anything on the heap, are exempt…
Richard
  • 993
  • 1
  • 7
  • 7
94
votes
3 answers

Why is a point-to-volatile pointer, like "volatile int * p", useful?

volatile is to tell the compiler not to optimize the reference, so that every read/write does not use the value stored in register but does a real memory access. I can understand it is useful for some ordinary variable, but don't understand how…
Infinite
  • 3,198
  • 4
  • 27
  • 36
94
votes
15 answers

Is it a good idea to typedef pointers?

I looked through some code and noticed that the convention was to turn pointer types like SomeStruct* into typedef SomeStruct* pSomeStruct; Is there any merit to this?
Unknown
  • 45,913
  • 27
  • 138
  • 182
93
votes
1 answer

Exceptions to array decaying into a pointer?

I have seen in many posts that "in most of the cases array names decay into pointers".Can I know in what cases/expressions the array name doesn't decay into a pointer to its first elements?
nj-ath
  • 3,028
  • 2
  • 25
  • 41
92
votes
7 answers

How to convert const char* to char* in C?

In my project there is a method which only returns a const char*, whereas I need a char* string, as the API doesn't accept const char*. Any idea how to convert between const char* to char*?
Morpheus
  • 3,285
  • 4
  • 27
  • 57
89
votes
17 answers

Why are references not reseatable in C++

C++ references have two properties: They always point to the same object. They can not be 0. Pointers are the opposite: They can point to different objects. They can be 0. Why is there no "non-nullable, reseatable reference or pointer" in C++? I…
TheFogger
  • 2,399
  • 1
  • 20
  • 22
88
votes
8 answers

What exactly is meant by "de-referencing a NULL pointer"?

I am a complete novice to C, and during my university work I've come across comments in code that often refer to de-referencing a NULL pointer. I do have a background in C#, I've been getting by that this might be similar to a…
Ash
  • 24,276
  • 34
  • 107
  • 152
88
votes
4 answers

Meaning of int (*) (int *) = 5 (or any integer value)

I cannot figure this out: int main() { int (*) (int *) = 5; return 0; } The above assignment compiles with g++ c++11. I know that int (*) (int *) is a pointer to a function that accepts an (int *) as argument and returns an int, but I do…
Konrad
  • 2,207
  • 4
  • 20
  • 31
88
votes
3 answers

How to convert a 'string pointer' to a string in Golang?

Is it possible to get the string value from a pointer to a string? I am using the goopt package to handle flag parsing and the package returns *string only. I want to use these values to call a function in a map. Example var strPointer =…
Jared Meyering
  • 1,291
  • 2
  • 11
  • 21
88
votes
19 answers

Checking if a pointer is allocated memory or not

Can we check whether a pointer passed to a function is allocated with memory or not in C? I have wriiten my own function in C which accepts a character pointer - buf [pointer to a buffer] and size - buf_siz [buffer size]. Actually before calling…
codingfreak
  • 4,467
  • 11
  • 48
  • 60