Home > Programming > Functions using the "C" programming language > example14.c

 

Previous

Next


/*
    In this example we demonstrate a function that returns a pointer to an integer value
*/
#include <stdio.h>
#include <conio.h>


/*
    Function prototype
*/

int *get_int_ptr(int *);

/*
    Main program
*/

void main()
{
    int x;
    x = 1;
    if (get_int_ptr( &x )!=NULL)
        { printf("OK, not negative.\n"); }
    else
        { printf("It is negative.\n"); }
    x = -1;
    if (get_int_ptr( &x )!=NULL)
        { printf("OK, not negative.\n"); }
    else
        { printf("It is negative.\n"); }
    getch();
}

/*
    Function get_int_ptr takes as parameter a pointer to an integer value.
    If the memory address strores a positive integer value or zero it returns the
    pointer parameter, else it returns NULL.
*/
int *get_int_ptr(int *x)
{
    if( *x >= 0) /* Check positive or zero */
        return x; /* Return value */
    else
        return NULL; /* Return NULL */
}


© 2004 Jim Valavanis

Previous

Next

1