Joe*_*Joe 0 c arrays malloc boolean header
我已经很久没和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
Run Code Online (Sandbox Code Playgroud)
标题来源:
/* 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);
}
Run Code Online (Sandbox Code Playgroud)
然后调用第二个源文件:
#include "arrayUtil.h"
int main(){
int * array = allocIntArray(20);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
当我编译时:
gcc utilTest.c
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
arrayUtils.h:10:错误:"已检查"之前的语法错误
最初我在accessIntArray中使用"bool checked",并且得到了相同的错误,但是使用bool而不是检查.
对不起,如果这不是一个特定的问题,但我在这里很丢失.
bool
未在C的C99语言标准的标准类型添加的新类型_Bool
为布尔数据类型,以及该头文件<stdbool.h>
定义bool
,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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
112 次 |
最近记录: |