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

Smart or raw pointers when implementing data structures?

I have been reading and experimenting with the standard library's smart pointers, unique_ptr and shared_ptr and although they are obviously great replacements for a lot of cases where raw pointers might be deemed dangerous I am unsure of their use…
dgouder
  • 339
  • 2
  • 11
12
votes
5 answers

Copy two structs in C that contain char pointers

What is the standard way to copy two structs that contain char arrays? Here is some code: #include stdio.h> #include string.h> #include stdlib.h> typedef struct { char* name; char* surname; } person; int main(void){ person p1; …
cateof
  • 789
  • 3
  • 11
  • 24
12
votes
3 answers

DotNet - What is int*?

simple question, I import a DLL function and the parameter are int*. When I try to enter Method(0), I get an error which says: "int and int* can not convert". What is that meaning?
PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176
12
votes
2 answers

Is there a way to check if a QObject-pointer is still valid in Qt?

I have a scenario where an anonymous QObject starts an asynchronous operation by emitting a signal. The receiving slot stores the QObject-pointer and sets a property of this object later. The object could be gone meanwhile. So, is there a safe way…
Valentin H
  • 7,240
  • 12
  • 61
  • 111
12
votes
4 answers

What's the difference between a VLA and dynamic memory allocation via malloc?

I was curious with this: What is the diference between: const int MAX_BUF = 1000; char* Buffer = malloc(MAX_BUF); and: char Buffer[MAX_BUF];
12
votes
6 answers

wchar_t pointer

What's wrong with this: wchar_t * t = new wchar_t; t = "Tony"; I thought I could use a wchar_t pointer as a string...
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
12
votes
2 answers

UnsafeMutablePointer to [UInt8] without memory copy

Is it possible to create a [UInt8] from an UnsafeMutablePointer without copying the bytes? In the NSData world I could simply call let data = NSData(bytesNoCopy: p, length: n, freeWhenDone: false) and just wrap the pointer.
tidwall
  • 6,881
  • 2
  • 36
  • 47
12
votes
1 answer

Why does asserting on the result of Deref::deref fail with a type mismatch?

The following is the Deref example from The Rust Programming Language except I've added another assertion. Why does the assert_eq with the deref also equal 'a'? Why do I need a * once I've manually called deref? use std::ops::Deref; struct…
12
votes
5 answers

Updating pointers in a function

I am passing a pointer a function that updates it. However when the function returns the pointer it returns to the value it had prior to the function call. Here is my code: #include #include static void func(char *pSrc) { …
asicman
  • 133
  • 1
  • 1
  • 4
12
votes
2 answers

C: When to return by value or pass reference

Although the subject is discussed many times, I haven't found any satisfying answer so far. When to return data from a function by return or to pass a reference to change the data on address? The classic answer is to pass a variable as reference to…
Yorick de Wid
  • 859
  • 11
  • 19
12
votes
6 answers

Dereferencing deleted pointers always result in an Access Violation?

I have a very simple C++ code here: char *s = new char[100]; strcpy(s, "HELLO"); delete [] s; int n = strlen(s); If I run this code from Visual C++ 2008 by pressing F5 (Start Debugging,) this always result in crash (Access Violation.) However,…
Gant
  • 29,661
  • 6
  • 46
  • 65
12
votes
1 answer

How to change the location of the pointer in python?

I want to paint some special words while the program is getting them , actually in real-time . so I've wrote this piece of code which do it quite good but i still have problem with changing the location of the pointer with move keys on keyboard and…
Mohammad Siavashi
  • 1,192
  • 2
  • 17
  • 48
12
votes
11 answers

How to determine the end of an integer array when manipulating with integer pointer?

Here is the code: int myInt[] ={ 1, 2, 3, 4, 5 }; int *myIntPtr = &myInt[0]; while( *myIntPtr != NULL ) { cout<<*myIntPtr<.......... For Character array: (Since we have a NULL character at the…
Akaanthan Ccoder
  • 2,159
  • 5
  • 21
  • 37
12
votes
5 answers

the "new" operator in c++, pointer question

Dumb question, but whenever you call new, do you always have a pointer? SomeClass *person = new SomeClass(); And is that because you need a pointer to point to that new space of memory that was allocated for the SomeClass variable person? Thanks!
Crystal
  • 28,460
  • 62
  • 219
  • 393
12
votes
1 answer

C - how to convert a pointer in an array to an index?

In the many search functions of C (bsearch comes to mind) if a result is found, a pointer to the spot in the array is returned. How can I convert this pointer to the index in the array that was searched (using pointer arithmetic, i assume).
Tony Stark
  • 24,588
  • 41
  • 96
  • 113