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
63
votes
3 answers

C vs C++ struct alignment

I've been asked in a recent interview about C++ struct fields alignment and theoretized that C and C++ follows the same strategy in struct packing. Hovewer, it was the wrong assumption. The interviewer said that in general C and C++ are packing…
Minor Threat
  • 2,025
  • 1
  • 18
  • 32
63
votes
3 answers

Hiding nil values, understanding why Go fails here

I fail to understand how to correctly assure that something is not nil in this case: package main type shower interface { getWater() []shower } type display struct { SubDisplay *display } func (d display) getWater() []shower { return…
sharpner
  • 3,857
  • 3
  • 19
  • 28
63
votes
3 answers

Function pointer as parameter

I try to call a function which passed as function pointer with no argument, but I can't make it work. void *disconnectFunc; void D::setDisconnectFunc(void (*func)){ disconnectFunc = func; } void D::disconnected(){ *disconnectFunc; …
Roland Soós
  • 3,125
  • 4
  • 36
  • 49
63
votes
20 answers

Determine the size of a C++ array programmatically?

This question was inspired by a similar question: How does delete[] “know” the size of the operand array? My question is a little different: Is there any way to determine the size of a C++ array programmatically? And if not, why? Every function…
Kip
  • 107,154
  • 87
  • 232
  • 265
63
votes
8 answers

Does it ever make sense to check if "this" is null?

Say I have a class with a member function; inside that method, I check this == nullptr, and if it is, return an error code. If this is null, then that means the object is deleted. Is the method even able to return anything? Update: I forgot to…
user156144
  • 2,215
  • 4
  • 29
  • 40
62
votes
6 answers

Why can't I convert 'char**' to a 'const char* const*' in C?

The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc & g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care): char **a; const char** b = a; I can understand and accept this. The…
HappyDude
  • 2,566
  • 3
  • 18
  • 14
62
votes
2 answers

How to create two classes in C++ which use each other as data?

I'm looking to create two classes, each of which contains an object of the other class type. How can I do this? If I can't do this, is there a work-around, like having each class contain a pointer to the other class type? Thanks! Here's what I…
Steve Johnson
  • 949
  • 3
  • 12
  • 13
62
votes
1 answer

How do I properly map a `MagImageScalingCallback` using JNA?

I am using jna.jar, jna-3.2.5.jar and jna-3.3.0-platform.jar in my Java Project. This is the Winapi function I want to replicate. BOOL WINAPI MagImageScalingCallback( _In_ HWND hwnd, _In_ void *srcdata, _In_ …
Vishnu
  • 11,614
  • 6
  • 51
  • 90
62
votes
5 answers

C/C++ int[] vs int* (pointers vs. array notation). What is the difference?

I know that arrays in C are just pointers to sequentially stored data. But what differences imply the difference in notation [] and *. I mean in ALL possible usage context. For example: char c[] = "test"; if you provide this instruction in a…
Alexander Reshytko
  • 2,126
  • 1
  • 20
  • 28
61
votes
9 answers

Are there pointers in php?

What does this code mean? Is this how you declare a pointer in php? $this->entryId = $entryId;
donalg d
61
votes
4 answers

What does ((void (*)())buf)(); mean?

I am solving a binary exploitation challenge on picoCTF and came across this piece of code: ((void (*)())buf)(); where buf is a character array. I solved the challenge but can't seem to understand what exactly it's doing. I looked at this thread…
sh.3.ll
  • 815
  • 7
  • 17
61
votes
1 answer

Why does C++11 contain an odd clause about comparing void pointers?

While checking the references for another question, I noticed an odd clause in C++11, at [expr.rel] ¶3: Pointers to void (after pointer conversions) can be compared, with a result defined as follows: If both pointers represent the same address or…
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
61
votes
6 answers

C: pointer to struct in the struct definition

How can I have a pointer to the next struct in the definition of this struct: typedef struct A { int a; int b; A* next; } A; this is how I first wrote it but it does not work.
claf
  • 9,043
  • 17
  • 62
  • 79
61
votes
6 answers

In C, are arrays pointers or used as pointers?

My understanding was that arrays were simply constant pointers to a sequence of values, and when you declared an array in C, you were declaring a pointer and allocating space for the sequence it points to. But this confuses me: the following…
salvador p
  • 2,905
  • 5
  • 26
  • 26
61
votes
4 answers

Using a C string gives a warning: "Address of stack memory associated with local variable returned"

I am not a C programmer, so I am not that familiar with C-string, but now I have to use a C library, so here is a shortened version of my code to demonstrate my problem: char** ReadLineImpl::my_completion () { char* matches[1]; matches[0] =…
khajvah
  • 4,889
  • 9
  • 41
  • 63