Jump to content

Pointers: Difference between revisions

From IdeaWazaWiki
wikademia>Rantir
Added two example pieces of code: one showing simple pointer creation and the other showing an example of pointers passed to functions.
wikademia>Rantir
Stubbed out future sections to provided examples for
Line 1: Line 1:
This page contains examples of pointers.  
This page contains examples of pointers.  


 
==Basic Pointers==
This example shows simple pointer creation, assignment and dereference.
This example shows simple pointer creation, assignment and dereference.
<source lang="c">
<source lang="c">
Line 66: Line 66:
</source>
</source>


 
==Basic Pointers with Functions==
This example shows how pointers can be used in functions. The code gives the reader some questions to consider and answer on their own.
This example shows how pointers can be used in functions. The code gives the reader some questions to consider and answer on their own.
<source lang="c">
<source lang="c">
Line 226: Line 226:


</source>
</source>
==Advanced Pointers==
These examples show how pointers can be used with malloc to point to blocks of memory.
Example:Pointers to structures.
Example: Pointers to void.
Example: Double, triple, quadruple pointers.
==Pointer Arithmetic==
==Advanced Pointers with Arrays==
Shows how arrays and pointers are really the same thing.
Example: Creating an array structure with pointers.
Example: Using pointer arithmetic to index array
Example: Using pointers to traverse array.
Example: Passing arrays to functions using pointers
==Function Pointers==

Revision as of 17:35, 5 April 2008

This page contains examples of pointers.

Basic Pointers

This example shows simple pointer creation, assignment and dereference. <source lang="c">

  1. include <stdio.h>

/*Example showing simple pointer assignment operations.

*/

int main(int argc, char *argv[]) {

  int *intPtr1;
  int *intPtr2;
  int a = 5;
  int b = 10;
  printf("\nBefore Initializing pointers.");
  printf("\nintPtr1: %d, *intPtr1: %d", intPtr1, *intPtr1);
  printf("\nintPtr2: %d, *intPtr2: %d\n", intPtr2, *intPtr2);
  /*Initialize the pointers*/
  intPtr1 = NULL;
  intPtr2 = NULL;
  printf("\nAfter Initializing pointers to NULL");
  printf("\nintPtr1: %d", intPtr1);
  printf("\nintPtr2: %d\n", intPtr2);
  intPtr1 = &a;
  intPtr2 = &b;
  printf("\nAfter pointing intPtr1 to the address of 'a' and "
        "\npointing intPtr2 to the address of 'b'\n");
  printf("\nintPtr1: %d, *intPtr1: %d", intPtr1, *intPtr1);
  printf("\nintPtr2: %d, *intPtr2: %d", intPtr2, *intPtr2);
  printf("\na: %d, b: %d\n", a, b);
  *intPtr1 = 7;
  *intPtr2 = 12;
  printf("\nAfter assigning 7 into the memory location pointed to by "
        "intPtr1 \n and assigning 12 into the memory location pointed to by intPtr2");
  printf("\nintPtr1: %d, *intPtr1: %d", intPtr1, *intPtr1);
  printf("\nintPtr2: %d, *intPtr2: %d", intPtr2, *intPtr2);
  printf("\na: %d, b: %d\n", a, b);
  intPtr1 = intPtr2;
  printf("\nAfter storing the memory location of intPtr2 into intPtr1. \nMakes intPtr2 now point to variable 'b' as well");
  printf("\nintPtr1: %d, *intPtr1: %d", intPtr1, *intPtr1);
  printf("\nintPtr2: %d, *intPtr2: %d", intPtr2, *intPtr2);
  printf("\na: %d, b: %d\n", a, b);
  *intPtr2 = 10;
  printf("\nAfter assigning '10' into the memory location pointed to by intPtr1 and intPtr2");
  printf("\nintPtr1: %d, *intPtr1: %d", intPtr1, *intPtr1);
  printf("\nintPtr2: %d, *intPtr2: %d", intPtr2, *intPtr2);
  printf("\na: %d, b: %d\n", a, b);
  b = 25;
  printf("\nAfter assigning '25' into the variable 'b'");
  printf("\nintPtr1: %d, *intPtr1: %d", intPtr1, *intPtr1);
  printf("\nintPtr2: %d, *intPtr2: %d", intPtr2, *intPtr2);
  printf("\na: %d, b: %d\n", a, b);

} </source>

Basic Pointers with Functions

This example shows how pointers can be used in functions. The code gives the reader some questions to consider and answer on their own. <source lang="c">

  1. include <stdio.h>

/*Example showing how pointers are used in functions.

*/

/* Only swaps the values within this fuction */ void badSwap(int c, int d); void goodSwap(int *c, int *d); void decrement(int *p); void increment(int *p);

void badSwap(int c, int d) {

  int temp = 0;
  printf("\nbadSwap");
  printf("\n\tBefore swapping in badSwap");
  printf("\n\ttemp: %d, c: %d, d: %d", temp, c, d);
  temp = c;
  c = d;
  d = temp;
  // Why won't this work?
  printf("\n\tAfter swapping in badSwap");
  printf("\n\ttemp: %d, c: %d, d: %d", temp, c, d);

}

/* Swaps the values using pointers*/ void goodSwap(int *c, int *d) {

  int temp = 0;
  printf("\ngoodSwap");
  printf("\n\tBefore swapping in goodSwap");
  printf("\n\ttemp: %d, c: %u, *c: %d, d: %u, *d: %d", temp, c, *c, d, *d);
  temp = *c;
  *c = *d;
  *d = temp;
  // Why does this work?
  printf("\n\tAfter swapping in goodSwap");
  printf("\n\ttemp: %u, c: %u, *c: %d, d: %u, *d: %d", temp, c, *c, d, *d);

}

void badSwap2(int *c, int *d) {

  int *temp = NULL;
  printf("\nbadSwap2");
  printf("\n\tBefore swapping in badSwap2");
  printf("\n\ttemp: %d, c: %u, *c: %d, d: %u, *d: %d", temp, c, *c, d, *d);
  temp = c;
  c = d;
  d = temp;
  // c and d have changed values! Great! Why won't their values be changed when
  // we return?
  printf("\n\tAfter swapping in badSwap2");
  printf("\n\ttemp: %u, c: %u, *c: %d, d: %u, *d: %d", temp, c, *c, d, *d);

}


/*Decrements the value pointed to by p */ void decrement(int *p) {

  // What we want *p = *p - 1;
  (*p)--;
  // What does *p-- do?

}


/*Increments the value pointed to by p*/ void increment(int *p) {

  int temp = p;
  // What we want *p = *p + 1
  (*p)++;
  // What does *p++ do?
  printf("\nincrement");
  printf("\n\t p: %u, *p: %u, &p: %u\n", p, *p, &p);
  p = p + 1;
  printf("\n\t p: %u, *p: %u, &p: %u\n", p, *p, &p);
  p = temp;
  p = *p + 1;
  printf("\n\t p: %u, &p: %u\n", p, &p);
  p = temp;
  p = &p + 1;
  printf("\n\t p: %u, *p: %u, &p: %u\n", p, *p, &p);
  p = temp;
  *p = p + 1;
  printf("\n\t p: %u, *p: %u, &p: %u\n", p, *p, &p);
  p = temp;
  *p = &p + 1;
  printf("\n\t p: %u, *p: %u, &p: %u\n", p, *p, &p);
  p = temp;

}

int main(int argc, char *argv[]) {

  int *aPtr;
  int *bPtr;
  int a = 5;
  int b = 10;
  /*Initialize the pointers*/
  aPtr = &a;
  bPtr = &b;
  /*Before call to badSwap*/
  printf("\nBefore call to badSwap");
  printf("\n\taPtr: %u, *aPtr: %d", aPtr, *aPtr);
  printf("\n\tbPtr: %u, *bPtr: %d", bPtr, *bPtr);
  printf("\n\ta: %d, b: %d\n", a, b);
  badSwap(a, b);
  /* After call to badSwap */
  printf("\n\nAfter call to badSwap");
  printf("\n\taPtr: %u, *aPtr: %d", aPtr, *aPtr);
  printf("\n\tbPtr: %u, *bPtr: %d", bPtr, *bPtr);
  printf("\n\ta: %d, b: %d\n", a, b);
  /*Before call to goodSwap*/
  printf("\nBefore call to goodSwap");
  printf("\n\taPtr: %u, *aPtr: %d", aPtr, *aPtr);
  printf("\n\tbPtr: %u, *bPtr: %d", bPtr, *bPtr);
  printf("\n\ta: %d, b: %d\n", a, b);
  goodSwap(aPtr, bPtr);
  goodSwap(&a, &b);
  /* After call to goodSwap */
  printf("\n\nAfter call to goodSwap");
  printf("\n\taPtr: %u, *aPtr: %d", aPtr, *aPtr);
  printf("\n\tbPtr: %u, *bPtr: %d", bPtr, *bPtr);
  printf("\n\ta: %d, b: %d\n", a, b);
  badSwap2(aPtr, bPtr);
  /* After call to badSwap2*/
  printf("\n\nAfter call to badSwap2");
  printf("\n\taPtr: %u, *aPtr: %d", aPtr, *aPtr);
  printf("\n\tbPtr: %u, *bPtr: %d", bPtr, *bPtr);
  printf("\n\ta: %d, b: %d\n", a, b);


  increment(&a);
  return 0;

}

</source>

Advanced Pointers

These examples show how pointers can be used with malloc to point to blocks of memory. Example:Pointers to structures. Example: Pointers to void. Example: Double, triple, quadruple pointers.

Pointer Arithmetic

Advanced Pointers with Arrays

Shows how arrays and pointers are really the same thing. Example: Creating an array structure with pointers. Example: Using pointer arithmetic to index array Example: Using pointers to traverse array. Example: Passing arrays to functions using pointers

Function Pointers