[英]variable length array alternative in c language
I have this code in c language, it does check if a number written in a certain numeric base, decimal, octal, ..etc is correct, means that it is using characters which belongs to this certain base, for example, an octal number should only use characters [0, 1, 2, 3, 4, 5, 6, 7], it checks all the bases between 2 and 36. 我用C语言编写此代码,它确实检查以某个数字基数,十进制,八进制,.. etc编写的数字是否正确,这意味着它使用的是属于该特定基数的字符,例如八进制数应该仅使用字符[0、1、2、3、4、5、6、7],它将检查2到36之间的所有底数。
The problem is that when I try to substring "base" characters from the total characters it give me a warning saying that ISO C90 forbids variable length array 'base_symbols'
问题是,当我尝试从总字符中对“基本”字符进行子字符串处理时,它会向我发出警告,指出
ISO C90 forbids variable length array 'base_symbols'
int checkNumBase(char *num, int base){
char all_symbols[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char base_symbols[base];
int i;
unsigned int k;
for(i = 0; i<base; i++){
base_symbols[i] = all_symbols[i];
}
for(k = 0; k<strlen(num); k++){
if(strchr(base_symbols, num[k]) == NULL){
return 0;
}
}
return 1;
}
One simple solution would be to truncate the string 一种简单的解决方案是截断字符串
char all_symbols[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // length corrected
if(base > 36)
return 0;
all_symbols[base] = 0;
//.. as before
ISO C90 forbids variable length array 'base_symbols'
ISO C90禁止使用可变长度数组'base_symbols'
There's nothing wrong with the code, you get error this because you are using an old, obsolete compiler. 代码没有任何错误,因为使用的是旧的,过时的编译器,所以会出现错误。 You need to get a modern one such as gcc.
您需要获得现代的产品,例如gcc。
Please note that older versions of gcc did support newer versions of the language if you compiled correctly: gcc -std=c11
or gcc -std=c99
, but used "gnu90" as default, which is C90 + non-standard extensions. 请注意,如果正确编译,则较旧版本的gcc确实支持该语言的较新版本:
gcc -std=c11
或gcc -std=c99
,但默认使用“ gnu90”,即C90 +非标准扩展名。 Newer versions 5.0 or later default to "gnu11". 较新的版本5.0或更高版本默认为“ gnu11”。
For example, -ansi
means "give me 30 years old crap mode" aka C90. 例如,
-ansi
意思是“给我30岁废话模式”(又名C90)。 Unless you really need C90 for backwards-compatibility reasons, you should be using gcc -std=c17 -pedantic-errors -Wall -Wextra
. 除非出于向后兼容的原因确实需要C90,否则应使用
gcc -std=c17 -pedantic-errors -Wall -Wextra
。
What is the difference between C, C99, ANSI C and GNU C? C,C99,ANSI C和GNU C有什么区别?
use char *index;
使用
char *index;
then index = strchr(all_symbols, toupper ( num[k]));
然后
index = strchr(all_symbols, toupper ( num[k]));
to see if the character is in the set 看角色是否在集合中
if index
is in the set it will have a larger address. 如果
index
在集合中,它将具有更大的地址。 subtract the smaller address from the larger address to get a positive result 从较大的地址中减去较小的地址以获得肯定的结果
then if ( index && index - all_symbols < base)
then num[k] is valid for that base. 然后,
if ( index && index - all_symbols < base)
则num [k]对于该基数有效。
toupper()
is in ctype.h
toupper()
在ctype.h
The solution by @WeatherVane (ie https://stackoverflow.com/a/55472654/4386427 ) is a very good solution for the code posted by OP. @WeatherVane提供的解决方案(即https://stackoverflow.com/a/55472654/4386427 )对于OP发布的代码而言是非常好的解决方案。
The solution below shows an alternative approach that doesn't use string functions. 下面的解决方案显示了一种不使用字符串函数的替代方法。
// Calculate the minimum base that allows use of char c
int requiredBase(char c)
{
if (c >= '0' && c <= '9') return c - '0' + 1; // '0' requires base 1, '1' requires base 2, ...
if (c >= 'A' && c <= 'Z') return c - 'A' + 11; // 'A' requires base 11, 'B'requires base 12, ...
return INT_MAX;
}
int checkNumBase(char *num, int base){
while (*num)
{
if (requiredBase(*num) > base) return 0;
++num;
}
return 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.