【发布时间】:2020-02-05 02:12:00
【问题描述】:
我目前正在尝试编译从 github 获取的 crc 计算器,但在 Visual Studio 2015 上编译时遇到困难。我收到错误预期常量表达式:
char paths[strlen(src) + 1 + strlen(name) + 2 + 1];
关于如何解决错误的想法?
static int create_source(char *src, char *name, FILE **head, FILE **code) {
// for error return
*head = NULL;
*code = NULL;
// create the src directory if it does not exist
int ret = _mkdir(src, 0755);
if (ret && errno != EEXIST)
return 1;
// construct the path for the source files, leaving suff pointing to the
// position for the 'h' or 'c'.
char paths[strlen(src) + 1 + strlen(name) + 2 + 1];
char *suff = stpcpy(path, src);
*suff++ = '/';
suff = stpcpy(suff, name);
*suff++ = '.';
suff[1] = 0;
// create header file
*suff = 'h';
*head = fopen(path, "wx");
if (*head == NULL)
return errno == EEXIST ? 2 : 1;
// create code file
*suff = 'c';
*code = fopen(path, "wx");
if (*code == NULL) {
int err = errno;
fclose(*head);
*head = NULL;
*suff = 'h';
unlink(path);
return err == EEXIST ? 2 : 1;
}
// all good -- return handles for header and code
return 0;
}
【问题讨论】:
-
使用支持 VLA 的更好的编译器。
-
或者分配存储并且根本不使用 VLA - 从 C11 开始,编译器现在可以选择支持(或不支持)。
-
path未定义
标签: c visual-studio-2015