首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >试图在C中创建一个基本的数组实用程序

试图在C中创建一个基本的数组实用程序
EN

Stack Overflow用户
提问于 2012-10-10 19:38:13
回答 2查看 212关注 0票数 0

我已经很久没有和C一起工作了,因此我忘记了关于C如何工作的令人尴尬的部分。我正在尝试创建一个标题'arrayUtils.h‘和相应的'arrayUtils.c’,其中我定义了原型函数。然后,我试图在第二个.c文件中调用其中一个函数

标题内容:

代码语言:javascript
运行
复制
#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

标题来源:

代码语言:javascript
运行
复制
/* 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);
}

然后调用第二个源文件:

代码语言:javascript
运行
复制
#include "arrayUtil.h"
int main(){
    int * array = allocIntArray(20);
    return(0);
}

当我用以下方式编译时:

代码语言:javascript
运行
复制
gcc utilTest.c

我知道这个错误:

arrayUtils.h:10:错误:“选中”之前的语法错误

最初,我在accessIntArray中使用了"bool“,并得到了相同的错误,但是使用bool而不是checked。

对不起,如果这不是一个具体的问题,但我在这里迷路了。

EN

Stack Overflow用户

发布于 2012-10-10 19:41:00

C没有'bool',您可能只是想使用int。或者C++,它确实具有布尔类型。

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12827047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档