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

How much does pointer indirection affect efficiency?

Is dereferencing a pointer notabley slower than just accessing that value directly? I suppose my question is - how fast is the deference operator?
user965369
  • 5,413
  • 13
  • 33
  • 47
13
votes
3 answers

Why is a pointer to a pointer incompatible with a pointer to an array?

OK, I'm having trouble understanding pointers to pointers vs pointers to arrays. Consider the following code: char s[] = "Hello, World"; char (*p1)[] = &s; char **p2 = &s; printf("%c\n", **p1); /* Works */ printf("%c\n", **p2); /* Segmentation fault…
Meta
  • 1,663
  • 2
  • 18
  • 29
13
votes
6 answers

Array of POINTERS to Multiple Types, C

Is it possible to have an array of multiple types by using malloc? EDIT: Currently I have: #include #include #include #define int(x) *((int *) x) int main() { void *a[10]; a[0] =…
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
13
votes
2 answers

Windows Task Manager Columns - Handles

What is the Windows Task Manager "Handles" column a measure of? File Handles? Or Page File Pointers? Also is it bad for one program to have 8000 handles?
patrick
  • 16,091
  • 29
  • 100
  • 164
13
votes
3 answers

How to pass Serial object by reference to my class in Arduino?

I have been reading up a couple of days now about pointers, references and dereferences in C/C++ targeted for the Arduino and can't fully udnerstand what I am missing. I have my sketch which has a setup of Serial.begin(9600); //Just for…
Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
13
votes
8 answers

Casting a pointer does not produce an lvalue. Why?

After posting one of my most controversial answers here, I dare to ask a few questions and eventually fill some gaps in my knowledge. Why isn't an expression of the kind ((type_t *) x) considered a valid lvalue, assuming that x itself is a pointer…
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
13
votes
6 answers

Must I use pointers for my C++ class fields?

After reading a question on the difference between pointers and references, I decided that I'd like to use references instead of pointers for my class fields. However it seems that this is not possible, because they cannot be declared uninitialized…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
13
votes
3 answers

What's char* const argv[]?

I'm studying linux C++ programing when I see int execve(const char *path, char *const argv[], char *const envp[]); I don't understand what is char *const argv[] . I know char *const foo is a const pointer to char. And const…
Kaoet
  • 367
  • 1
  • 4
  • 14
13
votes
4 answers

C free and struct

My question is about C free() function for deallocating memory blocks previously allocated with malloc(). If i have a struct data type compose of several pointers, each of them pointing to different memory locations, what would happen to those…
Leandro Galluppi
  • 915
  • 1
  • 7
  • 19
13
votes
5 answers

Assigning a reference by dereferencing a NULL pointer

int& fun() { int * temp = NULL; return *temp; } In the above method, I am trying to do the dereferencing of a NULL pointer. When I call this function it does not give exception. I found when return type is by reference it does not give…
G Mann
  • 441
  • 1
  • 4
  • 12
13
votes
7 answers

How to write a better strlen function?

I am reading "Write Great Code Volume 2" and it shows the following strlen impelementation: int myStrlen( char *s ) { char *start; start = s; while( *s != 0 ) { ++s; } return s - start; } the book says that this…
Victor
  • 1,655
  • 9
  • 26
  • 38
13
votes
4 answers

Pointer to const string in C

char *p = "string"; //creates pointer to constant string char p[] = "string"; //just an array with "string" I'm just a bit confused about why does it in the first example create a pointer to a constant string? Shouldn't it be just a pointer…
Mihai Neacsu
  • 2,045
  • 5
  • 23
  • 37
13
votes
3 answers

Tree with multiple child nodes and next node

I want to build a tree with the following characteristics: Every node can have 1 "next node". Every node can have multiple child nodes. The number of child nodes can vary from one node to the other I was thinking of a struct which looked like…
user560913
  • 315
  • 2
  • 6
  • 17
13
votes
6 answers

Function pointers and callbacks in C

I have started to review callbacks. I found this link on SO: What is a "callback" in C and how are they implemented? It has a good example of callback which is very similar to what we use at work. However, I have tried to get it to work, but I…
ant2009
  • 27,094
  • 154
  • 411
  • 609
13
votes
8 answers

Why do I have to use free on a pointer but not a normal declaration?

Why do I have to use free() when I declare a pointer such as: int *temp = (int*)malloc(sizeof(int)) *temp = 3; but not when I do: int temp = 3;
samoz
  • 56,849
  • 55
  • 141
  • 195