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
13
votes
2 answers

Passing a multidimensional array of variable size

I'm trying to understand what "best practice" (or really any practice) is for passing a multidimensional array to a function in c is. Certainly this depends on the application, so lets consider writing a function to print a 2D array of variable…
13
votes
1 answer

Incomplete array type int (*p_arr)[ ]?

When I compiled the following code with gcc -Wall -pedantic -ansi -std=c89, it compiled successfully without giving an error at the pointer assignment. Note that I convert from int (*)[4] to int (*)[]. int arr[4]; int (*p_arr)[] = &arr; Assuming…
user1969104
  • 2,340
  • 14
  • 15
13
votes
1 answer

Is it common to have struct members be pointers?

Take, for example, the go github package. Just about every member of every struct defined is a pointer to a value rather than a value. Is this idiomatic Go? Why? I understand that it reduces the size of the struct (assuming the size of the pointer…
AndrewH
  • 3,418
  • 2
  • 15
  • 27
13
votes
2 answers

How to move unique_ptr to raw pointer?

I have a function that is used to allocate a buffer with given size. The buffer will be preprocess/filled some data before return. This preprocess may return false to represent this buffer is not processed right. So I want to apply RAII on this…
Chen OT
  • 3,486
  • 2
  • 24
  • 46
13
votes
3 answers

Assigning pointer to lambda function to pointer to another lambda function

I am trying to assign a pointer to lambda function to pointer to another lambda function. The code will speak for itself: #include int main(int argc, char *argv[]) { auto l1 = []() { std::cout << "Lambda 1!" << std::endl; }; …
syntagma
  • 23,346
  • 16
  • 78
  • 134
13
votes
4 answers

Properly casting a `void*` to an integer in C++

I'm dealing with some code that uses an external library in which you can pass values to callbacks via a void* value. Unfortunately, the previous person working on this code decided to just pass integers to these callbacks by casting an integer to a…
Fake Name
  • 5,556
  • 5
  • 44
  • 66
13
votes
3 answers

MISRA incrementation in C

While debugging some embedded code, I came across something like this: buffPtr = &a[5]; buffEndPtr = &a[10]; while (buffPtr != buffEndPtr) { *buffPtr = 0xFF; buffPtr = &buffPtr[1]; /* MISRA improvement for: buffPtr++ */ } Why…
Adrian Suciu
  • 157
  • 1
  • 11
13
votes
6 answers

Not casting pointers in C can cause problems?

Yesterday I was in class, and at some point the instructor was talking about C code. He said: What is the purpose of making a pointer cast in C? The only purpose is to make the compiler interpret correctly the pointer operations (for example,…
felipeek
  • 1,193
  • 2
  • 10
  • 31
13
votes
3 answers

Convert string to *uint64 in golang

Assume there is a string holding the address of an uint64 type variable, can we parse this address back to an *uint64? For example: i := uint64(23473824) ip := &i str := fmt.Sprintf("%v", ip) u, _ := strconv.ParseUint(str, 0, 64) u is uint64. How…
ecem
  • 3,574
  • 3
  • 27
  • 40
13
votes
11 answers

Is this a memory leak?

char *pointer1; char *pointer2; pointer1 = new char[256]; pointer2 = pointer1; delete [] pointer1; In other words, do I have to do delete [] pointer2 as well? Thanks!
Ben
  • 133
  • 3
13
votes
2 answers

Casting pointer to memory buffer to pointer to VLA

in C, I believe the following program is valid: casting a pointer to an allocated memory buffer to an array like this: #include #include #define ARRSIZE 4 int *getPointer(int num){ return malloc(sizeof(int) * num); } int…
user3079266
13
votes
3 answers

C# default value of a pointer type

I have been searching through the C# language spec and I can't find anything which says whether a pointer type (e.g. int*) gets initialized with a default value. I created a simple test app and it appears to initialize them to zero but I'd like to…
user3784392
  • 131
  • 5
13
votes
3 answers

why is std::cout convertible to void* if using g++?

Why can one cast a std::ostream to a void pointer? I am not aware of any such conversion operator in std::ostream. Code below #include int main() { void *p = std::cout; // why does this work? } I'm asking this question since I've…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
13
votes
1 answer

When debugging Swift code, can I get a typed reference to an object given just its address?

When debugging Objective-C code in LLDB, I often create variables that refer to objects in memory using just their address. For example: (lldb) po self.view
Bill
  • 44,502
  • 24
  • 122
  • 213
13
votes
5 answers

What is "null pointer assignment error"?

One of job interview questions on C pointers here is the following: what is null pointer assignment error? I've googled for a while and don't see any reasonable explanation. What is that? Trying to write through a null pointer? Something…
sharptooth
  • 167,383
  • 100
  • 513
  • 979