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

Freeing pointers from inside other functions in C

Consider the c code: void mycode() { MyType* p = malloc(sizeof(MyType)); /* set the values for p and do some stuff with it */ cleanup(p); } void cleanup(MyType* pointer) { free(pointer); pointer = NULL; } Am I wrong in thinking that…
Ash
  • 24,276
  • 34
  • 107
  • 152
13
votes
7 answers

Why doesn't C++ enforce const on pointer data?

Possible Duplicate: Why isn't the const qualifier working on pointer members on const objects? Consider the following class that has a pointer member int *a. The const method constMod is allowed by the compiler even though it modifies the pointer…
Matthew Smith
  • 6,165
  • 6
  • 34
  • 35
13
votes
2 answers

C++: Comparing pointers of base and derived classes

I'd like some information about best practices when comparing pointers in cases such as this one: class Base { }; class Derived : public Base { }; Derived* d = new Derived; Base* b = dynamic_cast(d); // When comparing the two pointers…
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
13
votes
6 answers

Pointer to vector vs vector of pointers vs pointer to vector of pointers

Just wondering what you think is the best practice regarding vectors in C++. If I have a class containing a vector member variable. When should this vector be declared a: "Whole-object" vector member varaiable containing values, i.e.…
Lisa
  • 139
  • 1
  • 1
  • 3
13
votes
1 answer

Memory leak in golang slice

I just started learning go, while going through slice tricks, couple of points are very confusing. can any one help me to clarify. To cut elements in slice its given Approach 1: a = append(a[:i], a[j:]...) but there is a note given that it may…
Samarendra
  • 760
  • 10
  • 23
13
votes
4 answers

Tricky pointer question

I'm having trouble with a past exam question on pointers in c which I found from this link, http://www.cl.cam.ac.uk/teaching/exams/pastpapers/y2007p3q4.pdf The question is this: A C programmer is working with a little-endian machine with 8 bits…
user681007
13
votes
7 answers

Returning Arrays/Pointers from a function

I am trying to create a new integer array which is derived from a string of characters. For example : char x[] = "12334 23845 32084"; int y[] = { 12334, 23845, 32084 }; I am having trouble understanding how to return an array ( which I…
Garee
  • 1,274
  • 1
  • 11
  • 15
13
votes
2 answers

Does std::less have to be consistent with the equality operator for pointer types?

I've bumped into a problem yesterday, which I eventually distilled into the following minimal example. #include #include int main() { int i=0, j=0; std::cout << (&i == &j) << std::less()(&i,…
avakar
  • 32,009
  • 9
  • 68
  • 103
13
votes
3 answers

Using fseek with a file pointer that points to stdin

Depending on command-line arguments, I'm setting a file pointer to point either towards a specified file or stdin (for the purpose of piping). I then pass this pointer around to a number of different functions to read from the file. Here is the…
Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
13
votes
3 answers

Generally, is dereference pointer expression results a reference type?

Deferencing pointer leads to using the value of the object indirectly. But I've never really understood what does the "using" means. I started to think the question until my compiler yield an error for the following code int i = 0, *pi =…
SLN
  • 4,772
  • 2
  • 38
  • 79
13
votes
4 answers

Iterate through argv[]

I'm new to C and I completed a small exercise that iterates through the letters in an argument passed to it and identifies the vowels. The initial code only worked for one argument (argv[1]). I want to expand it to be able to iterate through all…
Jon Behnken
  • 560
  • 1
  • 3
  • 14
13
votes
1 answer

Swapping Property GetMethod implementation runtime

I'm currently trying to swap a property get implementation by replacing it with a bit of IL. I was using this question as reference: How to replace a pointer to a pointer to a method in a class of my method inherited from the system class? The only…
Stijn Bernards
  • 1,091
  • 11
  • 29
13
votes
5 answers

Modifying C string constants?

Possible Duplicate: Why do I get a segmentation fault when writing to a string? I want to write a function that reverses the given string passed into it. But, I can not. If I supply the doReverse function (see code below) with a character array,…
Dannnnny
13
votes
3 answers

Obtaining a pointer to the end of an array

I use the following template to obtain a pointer pointing after the last element of an array: template T* end_of(T (&array)[n]) { return array + n; } Now I seem to remember that there was some problem with this approach,…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
13
votes
1 answer

Why the second argument to pthread_join() is a **, a pointer to a pointer?

I am new to using pthread and also not that familiar with pointers to pointers. Could somebody perhaps explain why the second argument of pthread_join() is a void **. Why is it designed like this. int pthread_join(pthread_t thread, void…
artic sol
  • 349
  • 6
  • 18