0
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.

michael.r
  • 9
  • 1
  • `(int)token` does not give you the numeric value of whatever's in the string pointer returned from `strtok`, if that's what you're thinking it does. – Govind Parmar Nov 22 '18 at 00:41
  • You're casting an 8 byte pointer to a 4 byte int... but why? What are you trying to accomplish? – Shawn Nov 22 '18 at 00:43
  • @shawn, I was trying to make sure it is less than the restriction. I am new to c and still trying to figure things out. – michael.r Nov 22 '18 at 00:50

1 Answers1

0
(int)token > restrictions[0]

This, the cast of token to int is the reason you are gettingt that error message. token is a pointer to a zero terminated string which can't be easily compared to a number (restrictions[0]). You'll have to convert the string into a number, for example using 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]) {
    // ...
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43