PUBLIC void delset( set )
SET *set;
{
/* Delete a set created with a previous newset() call. */
if( set->map != set->defmap )
free( set->map );
free( set );
}
在C函数中是否一定需要有一个返回语句?上面的函数会抛出如下警告。请注意,我没有返回语句。
src/tools/set.c: In function ‘delset’:
src/tools/set.c:41:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
一旦添加了return 0;
,警告就会消失。 即使我只指定return;
也会显示警告。
PUBLIC void delset( set )
SET *set;
{
/* Delete a set created with a previous newset() call. */
if( set->map != set->defmap )
free( set->map );
free( set );
return 0;
}
我正在使用
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
。
编辑
看起来只是一个标记。这个SO问题似乎比我更好地解释了它的含义。void delset( set )
SET *set;
{
/* Delete a set created with a previous newset() call. */
if( set->map != set->defmap )
free( set->map );
free( set );
}
我将函数前面的PUBLIC标记和底部的返回行删除,但仍然出现相同的错误。
typedef struct _set_
{
unsigned char nwords; /* Number of words in map */
unsigned char compl; /* is a negative true set if true */
unsigned nbits; /* Number of bits in map */
_SETTYPE *map; /* Pointer to the map */
_SETTYPE defmap[ _DEFWORDS ]; /* The map itself */
} SET;
以下头文件(
debug.h
)的包含导致了问题。不确定具体是哪一行导致的。#ifdef DEBUG
# define PRIVATE
# define D (x) x
#else
# define PRIVATE static
# define D (x)
#endif
# define PUBLIC
#ifdef MSDOS
# define MS(x) x
# define UX(x)
# define ANSI
# define _8086
#else
# define MS(x)
# define UX(x) x
# define O_BINARY 0 /*no binary input mode in UNIX open() */
typedef long time_t; /* for the VAX, may have to change this */
typedef unsigned s1ze_t; /* for the VAX, may have to change this. Renamed the type as s1ze_t as stdio.h contains a type of the same name */
extern char *strdup(); /* You need to supply one. */
#endif
#ifdef ANSI /* If ANSI is defined, put arg lists into */
# define P(x) x /* function prototypes. */
# define VA_LIST ... /* and use ellipsis if a variable number of args */
#else
# define P(x) () /*Otherwise, discard argument lists and translate*/
# define void char /* void keyword to int. */
# define VA LIST _a_r_g_s /* don't use ellipsis */
#endif
/* SEG (p) Evaluates to the segment portion of an 8086 address.
* OFF (p) Evaluates to the offset portion of an 8086 address.
* PHYS (p) Evaluates to a long holding a physical address
*/
#ifdef _8086
# define SEG(p) ( ((unsigned *)&(p)) [1] )
# define OFF(p) ( ((unsigned *)&(p)) [0] )
# define PHYS(p) (((unsigned long)OFF(p)) + ((unsigned long)SEG(p) << 4))
#else
# define PHYS(p) (p)
#endif
/* NUMELE (array) Evaluates to the array size in elements
* LASTELE(array) Evaluates to a pointer to the last element
* INBOUNDS(array,p) Evaluates to true i f p points into the array.
* RANGE(a,b,c) Evaluates to true i f a <= b <= c
* max(a,b) Evaluates to a or b, whichever is larger
* min (a, b) Evaluates to a or b, whichever is smaller
*
* NBITS (type) Returns number of bits in a variable of the indicated
* type;
* MAXINT Evaluates to the value of the largest signed integer
*/
#define NUMELE(a) (sizeof(a)/sizeof(*(a)))
#define LASTELE(a) ((a) + (NUMELE(a)-1))
#define TOOHIGH(a,p) ((p) - (a) > (NUMELE(a) - 1))
#define TOOLOW(a,p) ( (p) - (a) < 0 )
#define INBOUNDS(a,p) (!(TOOHIGH(a,p) || TOOLOW(a,p))
#define _IS(t, x) (((t)1 << (x)) != 0) /* Evaluate true if the width of a */
/* variable of type of t is < x. The !=0 */
/* assures that the answer is 1 or 0 */
#define NBITS(t) (4 * (1 + _IS(t, 4) + _IS(t, 8) + _IS(t,12) + _IS(t, 16) + _IS(t,20) + _IS(t,24) + _IS(t,28) + _IS(t,32) )
#define MAXINT (((unsigned)~0) >> 1)
#ifndef max
# define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
# define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#define RANGE(a,b,c) ((a) <= (b) && (b) <= (c))
PUBLIC
是什么?公共的。在计算机编程中,PUBLIC
是一种关键字,用于表示可从程序的任何部分访问的变量、函数或类成员。 - alkvoid
函数来说这是必要的。如果你不想让函数返回一个值,就声明它为void
。这样控制流可以到达函数的结尾而不会出错(你也可以使用裸的return
语句)。如果函数是非void
的,那么它应该总是返回一个值。 - Tom Karzes# define void char /* void关键字变成了int。*/
。我会将其翻译为“将void关键字定义为char类型,/表示注释内容/”。请注意,该定义可能会导致代码出现问题,因为它违反了C语言规范。 - Abhineet