Subversion Repositories programming

Rev

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

Rev Author Line No. Line
87 irasnyd 1
/* arrays.c -- common functions for dealing with arrays
2
 *
3
 * Written By: Ira Snyder
4
 * Started On: 06-02-2005
5
 *
6
 * License: MIT License
7
 * License: http://www.opensource.org/licenses/mit-license.php
8
 *
9
 * _DO NOT_ forget to free the arrays you have created!
10
 */
86 irasnyd 11
 
12
#ifndef ARRAYS_C
13
#define ARRAYS_C
14
 
15
#include <stdio.h>  /* for printf() */
16
#include <stdlib.h> /* for exit() */
17
 
18
int* create_array (unsigned long size)
19
{
20
    int *array;
21
 
22
    if ( (array = (int*) malloc (size*sizeof(int))) == NULL)
23
    {
24
        printf ("Out of memory allocating array of size=%lu\n", size);
25
        printf ("Exiting...");
26
        exit (1);
27
    }
28
 
29
    return array;
30
}
31
 
32
int* free_array (int* array)
33
{
34
    free (array);
35
    array = NULL;
36
    return array;
37
}
38
 
39
/* copy the src[] to dst[] */
40
void copyarray (int src[], int dst[], unsigned long size)
41
{
42
    unsigned long i;
43
 
44
    for (i=0; i<size; i++)
45
        dst[i] = src[i];
46
}
47
 
48
/* copy src[src_left] to src[src_right] into dst[] */
49
void copyarray_partial (int src[], int dst[], unsigned long src_left, 
50
        unsigned long src_right)
51
{
52
    unsigned long i, j;
53
 
54
    for (i=src_left, j=0; i<=src_right; i++, j++)
55
        dst[j] = src[i];
56
}
57
 
58
/* print out the contents of the array
87 irasnyd 59
 * used for debugging mainly */
86 irasnyd 60
void printarray (int array[], unsigned long size)
61
{
62
    unsigned long i;
63
 
64
    printf("Printing %lu elements\n", size);
65
 
66
    for (i=0; i<size; i++)
67
        printf("%d ", array[i]);
68
 
69
    printf("\n");
70
}
71
 
72
#endif