【问题标题】:Trying to Create a Basic Array Utility in C尝试在 C 中创建基本数组实用程序
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • C99 在stdbool.h 中定义bool
【解决方案2】:

bool 不是 C 中的标准类型。C99 语言标准为布尔数据类型添加了新类型_Bool,以及定义booltrue 的头文件&lt;stdbool.h&gt;,和false 分别映射到_Bool(_Bool)1(_Bool)0

如果您使用 C99 编译器进行编译,请务必在使用 bool 关键字之前使用 #include &lt;stdbool.h&gt;。如果没有,请自行定义,例如:

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

【讨论】:

  • 哇,我真的很傻。我假设 GCC 是 C99,但我现在会坚持使用整数。有一个新问题是“未定义对 `allocIntArray' 的引用”我希望 GCC 能够自动编译和链接,我想我需要刷新 makefile。
  • @Joe,GCC 可以编译 C99 和 C89 代码(并且部分支持 C11)。要指定您的代码是 C99,您需要将 -std=c99 添加到 gcc 命令。请查看 standardsdialects 上的 GCC 页面了解更多信息。
  • 为了编译和链接你必须使用gcc arrayUtils.c utilTest.c -o utilTest
猜你喜欢
  • 2012-12-20
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
相关资源
最近更新 更多