pointers explained c code example

Example 1: how to make pointers in c

datatype *var;

variable var actually holds the address of the data(memory where it is stored)
*var lets you access the data stored at that address

Example 2: c function pointer

//Declaration of a pointer to a function that takes an integer 
//and returns an integer.
int (*f_ptr)(int);

//Assignment of a function foo to the function pointer f_ptr declared above.
f_ptr = foo;

//Calling foo indirectly via f_ptr, passing the return value of foo to r.
int r = f_ptr(v);

//Assigning an address of a function to the function pointer f_ptr,
//then calling foo by dereferencing the function pointer.  
f_ptr = &foo;
int r = (*f_ptr)(v);

Tags:

C Example