【发布时间】:2012-10-01 09:05:48
【问题描述】:
我已经很久没有使用过 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: error: "checked" 之前的语法错误
最初我在 accessIntArray 中使用“bool checked”,得到了同样的错误,但使用 bool 而不是 check。
对不起,如果这不是一个具体的问题,但我在这里很迷茫。
【问题讨论】:
-
const bool听起来是多余的,因为bool不是指针,因此更改或不更改它并不重要,因为无论如何您都在发送副本.函数所采用的const int也是如此。 -
Joe,我尝试复制您附加的确切标题内容,并尝试使用
g++编译它。我无法重现此问题。我不确定gcc,我认为它也不会在gcc中引发错误
标签: c arrays header boolean malloc