Jump to content

Pointers: Difference between revisions

From IdeaWazaWiki
wikademia>Gbaor
cat. change
wikademia>Rantir
→Advanced Pointers: Added basic example of doing a malloc to a block of memory
Line 228: Line 228:


==Advanced Pointers==
==Advanced Pointers==
These examples show how pointers can be used with malloc to point to blocks of memory.  
This example show how pointers can be used with malloc to point to blocks of memory.
 
<source lang="c">
#include <stdlib.h>
#include <stdio.h>
 
typedef unsigned char uchar;
typedef unsigned int uint;
 
/* This function shows how to malloc data. It shows that data might
* be zero upon initialization, but it is not guaranteed to be 0.
* */
void mallocExample();
 
void mallocExample() {
/* Using malloc */
uint size, i;
printf("\n Enter the desired size: ");
scanf("%d", &size);
uchar *data;
/* Check to make sure a NULL pointer wasn't returned from malloc */
if ((data = (uchar*) malloc(size)) == NULL)
exit(1);
 
/* Raw data after first malloc. Not guaranteed to be initialized*/
printf("\n After first malloc");
for (i = 0; i < size; i++)
printf("\n [%d]: %d", i, data[i]);
 
/* Fill */
for (i = 0; i < size; i++)
data[i] = i;
 
/* After fill */
printf("\n After fill");
for (i = 0; i < size; i++)
printf("\n [%d]: %d", i, data[i]);
 
/* When data is no longer needed it MUST be freed */
free(data);
 
if ((data = (uchar*) malloc(size)) == NULL)
exit(1);
/* Raw data after second malloc. Shows how there can be stuff 'left over'
* in memory after malloc call*/
printf("\n Raw data after malloc");
for (i = 0; i < size; i++)
printf("\n [%d]: %d", i, data[i]);
 
free(data);
}
 
int main(int argc, char **argv) {
mallocExample();
}
 
</source>
 
 
TODO:
Example:Pointers to structures.  
Example:Pointers to structures.  
Example: Pointers to void.
Example: Pointers to void.

Revision as of 02:11, 14 February 2009

This page contains examples of pointers. These examples assume the reader has the following knowledge: compiling C programs, variable creation and assignment, arrays, functions, basic I/O and an understanding of computer memory layout. These examples build from simple to more complex.

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

This example show how pointers can be used with malloc to point to blocks of memory.

<source lang="c">

  1. include <stdlib.h>
  2. include <stdio.h>

typedef unsigned char uchar; typedef unsigned int uint;

/* This function shows how to malloc data. It shows that data might

* be zero upon initialization, but it is not guaranteed to be 0.
* */

void mallocExample();

void mallocExample() { /* Using malloc */ uint size, i; printf("\n Enter the desired size: "); scanf("%d", &size); uchar *data; /* Check to make sure a NULL pointer wasn't returned from malloc */ if ((data = (uchar*) malloc(size)) == NULL) exit(1);

/* Raw data after first malloc. Not guaranteed to be initialized*/ printf("\n After first malloc"); for (i = 0; i < size; i++) printf("\n [%d]: %d", i, data[i]);

/* Fill */ for (i = 0; i < size; i++) data[i] = i;

/* After fill */ printf("\n After fill"); for (i = 0; i < size; i++) printf("\n [%d]: %d", i, data[i]);

/* When data is no longer needed it MUST be freed */ free(data);

if ((data = (uchar*) malloc(size)) == NULL) exit(1); /* Raw data after second malloc. Shows how there can be stuff 'left over' * in memory after malloc call*/ printf("\n Raw data after malloc"); for (i = 0; i < size; i++) printf("\n [%d]: %d", i, data[i]);

free(data); }

int main(int argc, char **argv) { mallocExample(); }

</source>


TODO: 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