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
14
votes
7 answers

Array increment operator in C

I don't understand the results of following code: #include #include int main() { int a[4]={1, 3, 5, 6}; //suppose a is stored at location 2010 printf("%d\n", a + 2); printf("%d", a++); return 0; } Why does the…
user221458
  • 716
  • 2
  • 7
  • 17
14
votes
2 answers

Do I need to free local variables?

I have the following struct: typedef struct cell Cell; struct cell { int value; int *nextcell; }; And I have the following function to free a linked list: void freelist(Cell *beginning) { Cell *thisCell = beginning; Cell *NextCell =…
Thi G.
  • 1,578
  • 5
  • 16
  • 27
14
votes
8 answers

Why does C support negative array indices?

From this post in SO, it is clear that C supports negative indices. Why support such a potential memory violation in a program? Shouldn't the compiler throw a Negative Index warning at least? (am using GCC) Or is this calculation done in…
boxed__l
  • 1,334
  • 1
  • 10
  • 24
14
votes
1 answer

Should I use pointers or move semantics for passing big chunks of data?

I have a questions about recommended coding technique. I have a tool for model analysis and I sometimes need to pass a big amount of data (From a factory class to one that holds multiple heterogeneous chunks). My question is whether there is some…
Adam Streck
  • 340
  • 2
  • 15
14
votes
4 answers

Safe to pass pointer to auto variable to function?

Suppose I have a function that declares and initializes two local variables – which by default have the storage duration auto. This function then calls a second function, to which it passes the addresses of these two local variables. Can this…
ravron
  • 11,014
  • 2
  • 39
  • 66
14
votes
4 answers

Function Pointers in Objective C

Quick question. Does anyone know how to get the function pointer of an objective c method? I can declare a C++ method as a function pointer, but this is a callback method so that C++ method would need to be part of the class SO THAT IT CAN ACCESS…
Sam Stewart
  • 1,470
  • 3
  • 14
  • 14
14
votes
5 answers

C - pointers and different results?

I cannot figure this out. Perhaps it is because it's 2am. At any rate, I am at a loss here. #include int main() { char array[] = "123456789"; char* ptr = array; printf("%c\n", *(ptr++)); printf("%c\n", *ptr); *ptr =…
sherrellbc
  • 4,650
  • 9
  • 48
  • 77
14
votes
9 answers

What are function pointers used for, and how would I use them?

I understand I can use pointers for functions. Can someone explain why one would use them, and how? Short example code would be very helpful to me.
zlack
  • 1,937
  • 4
  • 15
  • 15
14
votes
4 answers

Difference between int * array and int array[] in a function parameter

I have seen that the array values ​​will change if the function parameter is "int arr[]" or "int * arr". Where is the difference? int array[]: void myFunction(int arr[], int size) { for (int i = 0; i < size; ++i) arr[i] = 1; } int *…
Alex
  • 480
  • 1
  • 5
  • 15
14
votes
12 answers

initializing char pointers

I have a char pointer which would be used to store a string. It is used later in the program. I have declared and initialized like this: char * p = NULL; I am just wondering if this is good practice. I'm using gcc 4.3.3.
ant2009
  • 27,094
  • 154
  • 411
  • 609
14
votes
1 answer

From []byte to char*

I want to wrap a C function that takes a char* pointing to (the first element of) a non-empty buffer of bytes. I'm trying to wrap that in a Go function using CGo so that I can pass it a []byte, but I don't know how to do the conversion. A simplified…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
14
votes
1 answer

constexpr initializing with pointers

I am trying to initialize a constexpr declaration with a pointer to int which is a const object. I also try to define an object with a object that is not a const type. Code: #include int main() { constexpr int *np = nullptr; // np is a…
TheBlueCat
  • 1,147
  • 5
  • 19
  • 38
14
votes
9 answers

When to use pointers, and when not to use them

I'm currently doing my first real project in C++ and so, fairly new to pointers. I know what they are and have read some basic usage rules. Probably not enough since I still do not really understand when to use them, and when not. The problem is…
bastijn
  • 5,841
  • 5
  • 27
  • 43
14
votes
5 answers

If I have a void pointer, how do I put an int into it?

I have an array of arbitrary values, so I have defined it as an array of void pointers, so I can point to any kind of information (like int, character arrays, etc). However, how do I actually assign an int to it? Take for example these…
pbean
  • 727
  • 1
  • 10
  • 20
14
votes
3 answers

Difference between IntPtr and UIntPtr

I was looking at the P/Invoke declaration of RegOpenKeyEx when I noticed this comment on the page: Changed IntPtr to UIntPtr: When invoking with IntPtr for the handles, you will run into an Overflow. UIntPtr is the right choice if you wish this to…
xxbbcc
  • 16,930
  • 5
  • 50
  • 83