如何为旧的VisualStudio2005转换C代码,特别是TinyExpr源代码
我想转换的源代码 TinyExpr()在 Visual Studio 2005的C兼容代码(我认为是 C89/90兼容)。 特别是,我在转换此宏时遇到了一个问题(存在于tinyexpr.c中): 与:如何为旧的VisualStudio2005转换C代码,特别是TinyExpr源代码,c,visual-c++,C,Visual C++,我想转换的源代码 TinyExpr()在 Visual Studio 2005的C兼容代码(我认为是 C89/90兼容)。 特别是,我在转换此宏时遇到了一个问题(存在于tinyexpr.c中): 与: typedef struct te_expr { int type; union {double value; const double *bound; const void *function;}; void *parameters[1]; } te_expr; sta
typedef struct te_expr {
int type;
union {double value; const double *bound; const void *function;};
void *parameters[1];
} te_expr;
static te_expr *new_expr(const int type, const te_expr *parameters[]) {
...
}
static te_expr *base(state *s) {
...
}
调用时出现问题:NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE,base(s))代码>
我有一个:
错误C2059:sintax错误:“{”
正如我在一篇评论中所说的,在旧的编译器中确实没有很好的方法来实现这一点
一种可能的方法是使用wrapper vararg函数,它动态创建数组,调用realnew_expr
函数,然后释放临时数组
也许像
te_expr *new_expr_va(const int type, ...) {
re_expr *result = NULL; /* The resulting expression structure to return */
size_t argument_count = 0; /* The number of te_expr arguments passed */
/*
* We do two passes over the arguments, once to get the number of them,
* and once to get the actual values.
*/
/* First get the number of arguments */
{
va_list va;
va_start(va, type);
/* Loop until we get a null pointer */
while (va_arg(va, te_expr *) != NULL)
++argument_count;
va_end(va);
}
/* Now allocate the array */
te_expr **parameters = malloc(argument_count * sizeof(te_expr *));
/* And populate the array */
{
va_list va;
size_t index = 0;
te_expr *expr;
va_start(va, type);
/* Get all arguments and add to the allocated array */
while ((expr = va_arg(va, te_expr *)) != NULL)
parameters[index++] = expr;
va_end(va);
}
/* Now we call the actual function */
result = new_expr(type, parameters);
/* Free the memory we allocated for the array */
free(parameters);
/* And return the result */
return result;
}
可以使用例如
/* Argument list must be terminated by a NULL */
new_expr_va(TE_FUNCTION1 | TE_FLAG_PURE, base(s), NULL);
请注意,这些都未经测试,并且没有任何类型的错误检查。表达式(const te_expr*[]){{uu VA_ARGS}是一个,并没有一种很好的方法来回溯在旧的前C99编译器中使用的宏。VS2005现在已经很旧了,而且有更多的现代版本和最新的VisualStudio版本可以更好地处理C99。请考虑更新VisualStudio。您收到的实际错误消息,完整无误。请不要重新键入。@一些程序员谢谢您的建议,但目前我无法更新visual studio,该软件必须能够在visual studio 2005上工作。实际上,在较旧的编译器中根本不可能,因为vararg宏和VA_________
是一个C99添加项。您可以创建一个新的vararg函数new_expr\u va
,其中传递一个以null结尾的te_expr*
参数列表,它创建数组并调用实new_expr
函数,但无法创建如图所示的宏。@Someprogrammerdude请将代码放入答案中,以便我可以接受它,对我来说是这样的它是否是宏并不重要,我可以用任何代码替换它。我尝试过您的代码,但我有以下错误:1>\tinyexpr.c(117):警告C4013:“va\u start”未定义;假设extern返回int 1>\tinyexpr.c(120):警告C4013:“va\u arg”未定义;假设extern返回int 1>\tinyexpr.c(120):错误C2275:'te_expr':非法使用此类型作为表达式我很抱歉缺少#include,但“1>\tinyexpr.c(120):错误C2275:'te_expr':非法使用此类型作为表达式”错误仍然存在
/* Argument list must be terminated by a NULL */
new_expr_va(TE_FUNCTION1 | TE_FLAG_PURE, base(s), NULL);