Click on array_stack.c to get source.
/* File: CExamples/OO_pgm_in_C_new/array_stack.c */
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
/* This is the Data Structure used to implement the stack:
array[top-1] is the last element entered, array[0] the first */
typedef struct array_stackstruct
{ int top;
int array[20];
} array_stack, *array_stackptr;
stackptr new_stack()
/* This function is the interface from the abstract object to the D.S.
it creates a stack and the array_stack */
{stackptr newptr;
/* declare the functions that will implement the stack operations */
int array_isempty(stackptr);
void array_push (int, stackptr);
int array_pop (stackptr);
stackptr array_clone (stackptr);
printf("Constructing an array_stack\n");
/* allocate memory for the stack */
newptr = (stackptr)malloc(sizeof(stack));
/* set the function pointers to array functions */
newptr->is_empty = array_isempty;
newptr->push = array_push;
newptr->pop = array_pop;
newptr->clone = array_clone;
/* allocate memory for the array_stack and initialize */
newptr->objptr = malloc(sizeof(array_stack));
((array_stackptr)newptr->objptr)->top = 0;
/* must typecast as array_stackptr before selection: objptr has type void*;
note: conversion from and to type void* is permissible for all pointers */
return newptr;
}/* end new_stack */
void delete_stack( stackptr self )
/* Assumes that self is the result of new_stack */
{free(self->objptr); free(self);}/* end delete_stack */
/* The following three functions define the array_stack operations */
int array_isempty(stackptr self)
{return( ((array_stackptr)self->objptr)->top == 0 );
}/* end is_empty */
void array_push(int i, stackptr self)
{array_stackptr asptr;
asptr = (array_stackptr)self->objptr;
/* no check for overflow */
asptr->array[asptr->top++] = i;
}/* end array_push */
int array_pop(stackptr self)
{array_stackptr asptr;
asptr = (array_stackptr)self->objptr;
/* no check for underflow */
return asptr->array[--asptr->top];
}/* end array_pop */
stackptr array_clone (stackptr self)
{stackptr clone_ptr;
clone_ptr = (stackptr)malloc(sizeof(stack));
/* set the function pointers to array functions */
clone_ptr->is_empty = array_isempty;
clone_ptr->push = array_push;
clone_ptr->pop = array_pop;
clone_ptr->clone = array_clone;
clone_ptr->objptr = (array_stackptr)malloc(sizeof(array_stack));
*((array_stackptr)clone_ptr->objptr) = *((array_stackptr)self->objptr); /* field-wise assign */
return clone_ptr;
}/* end array_clone */