我已经很久没和C一起工作了,因此我忘记了关于C如何工作的尴尬。我正在尝试创建一个标头'arrayUtils.h'和一个相应的'arrayUtils.c',我在其中定义了原型函数。然后我试图在第二个.c文件中调用其中一个函数
标题内容:
#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
标题来源:
/* 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);
}
然后调用第二个源文件:
#include "arrayUtil.h"
int main(){
int * array = allocIntArray(20);
return(0);
}
当我编译:
gcc utilTest.c
我收到此错误:
arrayUtils.h:10:错误:“已检查”之前的语法错误
最初我在accessIntArray中使用“bool checked”,并且得到了相同的错误但是使用bool而不是check。
很抱歉,如果这不是一个特定的问题,但我很遗憾。
答案 0 :(得分:5)
bool
不是C中的标准类型.C99语言标准为布尔数据类型添加了新类型_Bool
,以及定义{{<stdbool.h>
的头文件bool
。 1}},true
和false
分别映射到_Bool
,(_Bool)1
和(_Bool)0
。
如果您正在使用C99编译器进行编译,请务必在使用#include <stdbool.h>
关键字之前bool
。如果没有,请自己定义,例如:
typedef unsigned char bool; // or 'int', whichever you prefer
#define true ((bool)1)
#define false ((bool)0)
答案 1 :(得分:2)
C没有'bool',你可能只想使用int。或C ++,它有布尔类型。