Pointers and functions



Pointers can be used with functions. The main use of pointers is ‘call by reference’ functions. Call by reference function is a type of function that has pointers (reference) as parameters to that function. All calculation of that function will be directly performed on referred variables.

Example :
void DoubleIt(int *num)
  {
    *num*=2;
  }
  int main()
  {
      int number=2;
      DoubleIt(&number);
      printf("%d",number);
      return 0;
  }

Pointer to Arrays

Array identifier is a pointer itself. Therefore & notation shouldn’t be used with arrays. The example of this can be found at code 3. When working with arrays and pointers always remember the following

  • Never use & for pointer variable pointing to an array.
  • When passing array to function you don’t need * for your declaration.
  • Be VERY CAREFUL not to exceed number of elements your array holds when using pointer arithmetic to avoid errors.

Pointers and Structures

Pointers and structures is broad topic and it can be very complex to include it all in one single tutorial. However pointers and structures are great combinations; linked lists, stacks, queues and etc are all developed using pointers and structures in advanced systems.

Example : Number Structure
struct details {
    int num;
};
int main()
{
    struct details MainDetails;
    struct details *structptr;
    structptr=&MainDetails;
    structptr->num=20;
    printf("n%d",MainDetails.num);
    return 0;
}