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

Correct format specifier to print pointer or address?

Which format specifier should I be using to print the address of a variable? I am confused between the below lot. %u - unsigned integer %x - hexadecimal value %p - void pointer Which would be the optimum format to print an address?
San
  • 3,933
  • 7
  • 34
  • 43
259
votes
12 answers

Why is the asterisk before the variable name, rather than after the type?

Why do most C programmers name variables like this: int *myVariable; rather than like this: int* myVariable; Both are valid. It seems to me that the asterisk is a part of the type, not a part of the variable name. Can anyone explain this logic?
WBlasko
  • 2,893
  • 2
  • 17
  • 11
257
votes
9 answers

Should I use static_cast or reinterpret_cast when casting a void* to whatever

Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other?
Andy
  • 3,004
  • 2
  • 22
  • 12
257
votes
19 answers

Passing by reference in C

If C does not support passing a variable by reference, why does this work? #include void f(int *j) { (*j)++; } int main() { int i = 20; int *p = &i; f(p); printf("i = %d\n", i); return 0; } Output: $ gcc -std=c99 test.c $…
aks
  • 4,695
  • 8
  • 36
  • 37
251
votes
17 answers

Is the sizeof(some pointer) always equal to four?

For example: sizeof(char*) returns 4. As does int*, long long*, everything that I've tried. Are there any exceptions to this?
Joel
  • 15,166
  • 16
  • 39
  • 31
243
votes
4 answers

Which kind of pointer do I use when?

Ok, so the last time I wrote C++ for a living, std::auto_ptr was all the std lib had available, and boost::shared_ptr was all the rage. I never really looked into the other smart pointer types boost provided. I understand that C++11 now provides…
sbi
  • 219,715
  • 46
  • 258
  • 445
233
votes
6 answers

How come an array's address is equal to its value in C?

In the following bit of code, pointer values and pointer addresses differ as expected. But array values and addresses don't! How can this be? Output my_array = 0022FF00 &my_array = 0022FF00 pointer_to_array = 0022FF00 &pointer_to_array =…
Alexandre
  • 5,035
  • 7
  • 29
  • 36
232
votes
3 answers

Value receiver vs. pointer receiver

It is very unclear for me in which case I would want to use a value receiver instead of always using a pointer receiver. To recap from the docs: type T struct { a int } func (tv T) Mv(a int) int { return 0 } // value receiver func (tp…
Chrisport
  • 2,936
  • 3
  • 15
  • 19
223
votes
24 answers

What exactly is a C pointer if not a memory address?

In a reputable source about C, the following information is given after discussing the & operator: ... It's a bit unfortunate that the terminology [address of] remains, because it confuses those who don't know what addresses are about, and misleads…
d0rmLife
  • 4,112
  • 7
  • 24
  • 33
209
votes
14 answers

How do pointer-to-pointers work in C? (and when might you use them?)

How do pointers-to-pointers work in C? When might you use them?
debugger
209
votes
8 answers

Returning an array using C

I am relatively new to C and I need some help with methods dealing with arrays. Coming from Java programming, I am used to being able to say int [] method() in order to return an array. However, I have found out that with C you have to use pointers…
user1506919
  • 2,377
  • 5
  • 20
  • 15
204
votes
3 answers

Why is 'this' a pointer and not a reference?

I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments. programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type…
Naveen
  • 74,600
  • 47
  • 176
  • 233
203
votes
14 answers

Can I use if (pointer) instead of if (pointer != NULL)?

Is it safe to check a pointer to not being NULL by writing simply if(pointer) or do I have to use if(pointer != NULL)?
danijar
  • 32,406
  • 45
  • 166
  • 297
202
votes
13 answers

C isn't that hard: void ( *( *f[] ) () ) ()

I just saw a picture today and think I'd appreciate explanations. So here is the picture: Transcription: "C isn't that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions…
Motun
  • 2,149
  • 3
  • 16
  • 23
199
votes
2 answers

" is pointer to interface, not interface" confusion

I have this problem which seems a bit weird to me. Take a look at this snippet of code: package coreinterfaces type FilterInterface interface { Filter(s *string) bool } type FieldFilter struct { Key string Val string } func (ff…
0rka
  • 2,246
  • 2
  • 11
  • 20