Examples of Pointer Declaration and Use

The following program shows how you can pass a pointer to a function and change the value of the object the pointer points to:

/***************************************************************
**  This program accepts a value for a timer, then decreases  **
**  this timer value by one each time the function count_down **
**  is called.                                                **
***************************************************************/
 
#include <stdio.h>
 
int count_down(int *timer)
int main(void)
{
   int t_timer;               /* local storage */
 
   printf("Set timer to: _ \n");
   scanf("%d", &t_timer);
   if (t_timer <= 0)
      printf("Timer was set to a negative value\n"); 
   else 
   { 
      while ( count_down(&t_timer) ) /* while timer not zero */ 
      { 
         printf("Timer still counting. %d\n", t_timer); 
      } 
      printf("Timer has reached zero.\n"); 
   } 
} /* End main */ </pre>

 

/***************************************************************
** This function decreases the value of timer by decrements   **
** of 1 and returns false when the timer reaches zero.        **
***************************************************************/
 
int count_down(int *timer)    /* receives a copy of a pointer to
                                      t_timer */
{
   return(--*timer);           /* modifying t_timer in main */
}   /* End count_down */

 

Interaction with this program could produce the following sessions:

Output Set timer to: _
Input 6
Output Timer still counting. 5
Timer still counting. 4
Timer still counting. 3
Timer still counting. 2
Timer still counting. 1
Timer has reached zero.

The following program contains pointer arrays:

/**************************************************************
** Program to search for the first occurrence of a specified **
** character string in an array of character strings.        **
**************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define  SIZE  20
#define  EXIT_FAILURE 999
 
int main(void)
{
   static char *names[ ] = { "Jim", "Amy", "Mark", "Sue", NULL };
   char * find_name(char **, char *);
   char new_name[SIZE], *name_pointer;
 
   printf("Enter name to be searched.\n");
   scanf("%s", new_name);
   name_pointer = find_name(names, new_name);
   printf("name %s%sfound\n", new_name,
          (name_pointer == NULL) ? " not " : " ");
   exit(EXIT_FAILURE);
} /* End of main */

/*************************************************************
**  Function find_name.  This function searches an array    **
**  of names to see if a given name already exists in the   **
**  array.  It returns a pointer to the name or NULL if     **
**  the name is not found.                                  **
**                                                          **
**  char **arry is a pointer to arrays of pointers whose    **
**  names already exist.                                    **
**                                                          **
**  char *strng is a pointer to character array entered     **
*************************************************************/
 
char * find_name(char **arry, char *strng)
{
   for (; *arry != NULL; arry++)     /* for each name       */
   {
      if (strcmp(*arry, strng) == 0) /* if strings match    */
         return(*arry);              /* found it!           */
   }
   return(*arry);                    /* return the pointer  */
} /* End of find_name */

Interaction with this program could produce the following sessions:

Output Enter name to be searched.
Input Mark
Output name Mark found

or:

Output Enter name to be searched._
Input Deborah
Output name Deborah not found


Pointer Type