Question

I'm having a hard time understanding pointers, particularly function pointers, and I was hoping someone could give me a rundown of exactly what they are and how they should be used in a program. Code blocks in C++ would be especially appreciated.

Thank you.

Was it helpful?

Solution

The concept of indirection is important to understand.

Here we are passing by value (note that a local copy is created and operated on, not the original version) via increment(x):

pass by value

And here, by pointer (memory address) via increment(&x):

pass by pointer

Note that references work similarly to pointers except that the syntax is similar to value copies (obj.member) and that pointers can point to 0 ("null" pointer) whereas references must be pointing to non-zero memory addresses.

Function pointers, on the other hand, let you dynamically change the behaviour of code at runtime by conveniently passing around and dealing withh functions in the same way you would pass around variables. Functors are often preferred (especially by the STL) since their syntax is cleaner and they let you associate local state with a function instance (read up about callbacks and closures, both are useful computer science concepts). For simple function pointers/callbacks, lambdas are often used (new in C++11) due to their compact and in-place syntax.

OTHER TIPS

The pointer points to the point, is an integer value that has the address of that point. Pointers can point to other pointers. Then you can get the values more-indirect way.

Reference operator (&): You may equate a pointer to a reference of a variable or a pointer.

Dereference operator (*): You may get the value of cell pointed by the pointer.

Arrays are decayed into a pointer when passed to a function by not a reference.

Function pointers are not inlined and makes program more functional. Callback is an example to this.


enter image description here


enter image description here


enter image description here


enter image description here


enter image description here

Here you can find some example uses of Function pointer: http://www.cprogramming.com/tutorial/function-pointers.html

As an analogy, think of the memory in the computer as an Excel sheet. The equivalent of assigning a value to a variable in a C/C++ program would be to write something into a cell on the Excel sheet. Reading from a variable would be like looking at a cell's content.

Now, if you have a cell (say C3) whose content is "B8" you can interpret that content as a reference to another cell. If you treat the cell C3 in that manner, C3 becomes like a pointer. (In Excel, you can actually achieve this behavior by entering =B8 into C3).

In such a scenario, you basically state that the cell whose value you're interested in is referenced in C3. In C++, this could be something like:

int  B8 =  42;
int* C3 = &B8;

You now have two variables that occuppy memory. Now, if you want to know what C3 points to, you'll use

int my_value = *C3;

As for function pointers: these are variables like ordinary pointers but the address (cell) they point to is not just a value but rather a function you can call.

In order to understand pointers one needs to understand a bit about hardware and memory layout.

Computer memory can be seen as a cupboard with drawers. A pointer can point to an arbitrary drawer, when you "dereference" the pointer you are looking inside the drawer, i.e. the value stored in the "drawer":


e.g. ten numbers stored after one another

short a[] = {9,2,3,4,5,6,7,8,1,-1] ; 

in memory the values that consist the array 'a' are stored sequentially

   +---+---+---+---+---+---+---+---+---+---+
a->| 9 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | -1|
   +---+---+---+---+---+---+---+---+---+---+

the 'a' array above is a pointer and is the start address where the
values are stored in memory.

short* ptr = 0; // a pointer not pointing to anything (NULL)

ptr = a + 5; // the pointer is now pointing to the 6th value in the array
// the + 5 is an offset on the starting address of a and since the type of
// a is short int array the compiler calculates the correct byte offset based
// on that type. in the above example 5 is 5 short ints since ptr is of type 
// short*

*ptr has the value 6 i.e. we are looking at what the ptr is pointing to.

The size of each "drawer" is determined of the data type that is stored In the above example 10 short ints are stored, every short int occupies 2 bytes so the whole array occupies 20 bytes of memory (sizeof(a))

Variables store values. Pointers store values too. The only difference is that they are memory addresses. They are still numbers. Nothing complicated here. You could store all pointers in int or long variables:

  int p = 43567854;
  char *p1 = (char *) p;

But advantage of storing them in pointer variables is that you can describe what type of variable is saved at the address the pointer points to.

What gets complicated about pointers is the cryptic syntax you have to use with them. Syntax is cryptic so that it's short to type. Like:

  &p = return address of variable
  *p = return the value of first member of array that is stored at the address

By using the two rules above we can write this cryptic code:

  &(*++t)

Which translated into human language gets quite long: Increase value of t by 1. this now points to second member of array of values pointer points to. then get value of second member (*) and then get address of this value. if we print this pointer it will print whole string except the first character.

You should make a "pointers syntax cheat sheet.txt" and you are good. And have open "Pointers tests" projects to test everythng that is unclear to you.

Pointers are similar to regular expressions in a way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top