Joe
Joe

Reputation: 340

Trying to Create a Basic Array Utility in C

I haven't worked with C in ages and as such I've forgotten an embarrassing amount about how C works. I am trying to create a header, 'arrayUtils.h' and a corresponding 'arrayUtils.c' where I define the prototyped functions. Then I am trying to call one of these functions in a second .c file

Header Contents:

#define _OUT_OF_RANGE_ = NAN

#ifndef INT_ALLOCATE_H
#define INT_ALLOCATE_H
int * allocIntArray(const int size);
#endif

#ifndef INT_ACCESS_H
#define INT_ACCESS_H
int accessIntArray(const int index, const int * array, const bool checked);
#endif

#ifndef INT_FREE_H
#define INT_FREE_H
int freeIntArray(int * array);
#endif

Source for header:

/* Allocates an array of integers equal to length size
 * Args: int size: length of the array
 * Return: Allocated array
 */
int * allocIntArray(const int size){
    /*Assert that size of array is greater than zero*/
    if(size <= 0){
        return(-1);
    }
    else{
        return((int*)malloc(size*sizeof(int)));
    }
}
/* Returns the value of the array 
 * Args: int    index:   position in the array to access
 *       int  * array:   array to access
 *       bool   checked: if the access should be checked or not
 * Returns: integer at position index
 */
int accessIntArray(const int index, const int * array, const bool checked){
    /*unchecked access*/
    if(!checked){
        return(array[index]);
    }
    /*checked access*/
    else{
        if(index <= 0){
            return(_OUT_OF_RANGE_)
        }
        double size = (double)sizeof(array)/(double)sizeof(int)
        if(index => (int)size){
            return(_OUT_OF_RANGE_)
        }
        else{
            return(array[index])
        }
    }
}

/* Frees the allocated array 
 * Args: int * array: the array to free
 * Returns: 0 on successful completion
 */
int freeIntArray(int * array){
    free(array);
    return(0);
}

Then calling in a second source file:

#include "arrayUtil.h"
int main(){
    int * array = allocIntArray(20);
    return(0);
}

When I compile with:

gcc utilTest.c

I get this error:

arrayUtils.h:10: error: syntax error before "checked"

Initially I was using "bool checked" in accessIntArray, and got the same error but with bool instead of checked.

Sorry if this isn't a specific question but I'm pretty lost here.

Upvotes: 0

Views: 260

Answers (2)

Adam Rosenfield
Adam Rosenfield

Reputation: 400224

bool is not a standard type in C. The C99 language standard added the new type _Bool for a boolean data type, as well as the header file <stdbool.h> which defines bool, true, and false to map to _Bool, (_Bool)1, and (_Bool)0 respectively.

If you're compiling using a C99 compiler, just be sure to #include <stdbool.h> before using the bool keyword. If not, define them yourselves, e.g.:

typedef unsigned char bool;  // or 'int', whichever you prefer
#define true ((bool)1)
#define false ((bool)0)

Upvotes: 5

Mark Stevens
Mark Stevens

Reputation: 2366

C doesn't have a 'bool', you probably just want to use an int. Or C++, which does have boolean types.

Upvotes: 2

Related Questions