我有一个指向char数组的指针.我想增加它,直到它不指向数字/数字(在char数组中表示为char).
例如,如果pointer指向'2'此char数组:
['1'][' ']['2']['3']['4'][' '][' '][' ']['5']['6']['7']
^
*pointer
Run Code Online (Sandbox Code Playgroud)
我想增加它,直到它指向第一个非数字字符 - ' ':
['1'][' ']['2']['3']['4'][' '][' '][' ']['5']['6']['7']
^
*pointer
Run Code Online (Sandbox Code Playgroud)
我知道我可以这样做:
while (*pointer == '0' || *pointer == '1' || *pointer == '2' || ...)
pointer++;
return pointer;
Run Code Online (Sandbox Code Playgroud)
但它很长很不优雅.
我以为我可以使用atoi(),0当指针没有指向数字时返回:
while (atoi(pointer) != 0 || *pointer == '0') //while it still points at a number
pointer++; //increase the pointer until it will not point at a number
return pointer;
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.也许没关系,我在其他地方有错,但无论如何我想知道:
是否还有其他(更好的)方法来检查指向char数组的指针是否指向一个数字/数字并增加它直到它指向一个非数字字符,在C?
你应该使用isdigit的ctype.h替代.就像是:
while (*pointer && isdigit(*pointer))
pointer++;
Run Code Online (Sandbox Code Playgroud)