Subversion Repositories programming

Rev

Rev 87 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/* arrays.c -- common functions for dealing with arrays
 *
 * Written By: Ira Snyder
 * Started On: 06-02-2005
 *
 * License: MIT License
 * License: http://www.opensource.org/licenses/mit-license.php
 *
 * _DO NOT_ forget to free the arrays you have created!
 */

#ifndef ARRAYS_C
#define ARRAYS_C

#include <stdio.h>  /* for printf() */
#include <stdlib.h> /* for exit() */

int* create_array (unsigned long size)
{
    int *array;

    if ( (array = (int*) malloc (size*sizeof(int))) == NULL)
    {
        printf ("Out of memory allocating array of size=%lu\n", size);
        printf ("Exiting...");
        exit (1);
    }

    return array;
}

int* free_array (int* array)
{
    free (array);
    array = NULL;
    return array;
}

/* copy the src[] to dst[] */
void copyarray (int src[], int dst[], unsigned long size)
{
    unsigned long i;

    for (i=0; i<size; i++)
        dst[i] = src[i];
}

/* copy src[src_left] to src[src_right] into dst[] */
void copyarray_partial (int src[], int dst[], unsigned long src_left, 
        unsigned long src_right)
{
    unsigned long i, j;

    for (i=src_left, j=0; i<=src_right; i++, j++)
        dst[j] = src[i];
}

/* print out the contents of the array
 * used for debugging mainly */
void printarray (int array[], unsigned long size)
{
    unsigned long i;
       
    printf("Printing %lu elements\n", size);
        
    for (i=0; i<size; i++)
        printf("%d ", array[i]);

    printf("\n");
}

#endif