简体   繁体   English

从指针强制转换为不同大小的整数[-Wpointer-to-int-cast]

[英]cast from pointer to integer of different size [-Wpointer-to-int-cast]

bool is_legal_size_spec(char *timeSpec){

  char timeSpecArray[100];
  strcpy(timeSpecArray, timeSpec);
  char restrictions[] = {100,12,31,24,60,60};
  char delimiter[] = " yndhms";

  char *token = strtok(timeSpecArray, delimiter);
  printf("%s", token);

  if((int)token > restrictions[0]){
    printf("%s", "No");
    return 0;
  }

I have to compare token to the first element in restrictions, but I am getting an error message that says cast from pointer to integer of different size, does anyone have any suggestions? 我必须将令牌与限制中的第一个元素进行比较,但是我收到一条错误消息,提示从指针转换为不同大小的整数,有人有什么建议吗? I have looked around and couldn't find anything. 我环顾四周,找不到任何东西。 Anything is appreciated, thanks. 谢谢,谢谢。

 (int)token > restrictions[0] 

This, the cast of token to int is the reason you are gettingt that error message. 这就是将tokenint的原因,是您收到该错误消息的原因。 token is a pointer to a zero terminated string which can't be easily compared to a number ( restrictions[0] ). token是指向零终止字符串的指针,该字符串不容易与数字进行比较( restrictions[0] )。 You'll have to convert the string into a number, for example using strtol() : 您必须将字符串转换为数字,例如使用strtol()

#include <errno.h>
#include <stdlib.h>

// ...

char *end;
errno = 0;
long value = strtol(token, &end, 10);
if(token == end || errno == ERANGE) {
    // conversion failed :(
}
else if (value > restrictions[0]) {
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM