Hi, i'm trying to make 3 different input scenarios using argv. In the code below where there is just one input so when it is argv[0] the calendar for this month comes up, when it is argv[1] so when there are two inputs, it will printout the calendar for the whole year and when it is argv[2] it will printout the calendar for a specific year and month. but i'm getting this warning error at (int year = argv[1]) and (int month = argv[1]) and (int year = argv[2]) and i'm not sure where to start fixing it:

int main(int argc, char* argv[]){
    if(argc==2){
        int year = argv[1];
        if(year<1900 || year>9999){
            printf("%d not in range 1900...9999", year);
            return 0;
        }
        calendarYear(year);
        return 0;
    }
    else if(argc>2){
        int month = argv[1];
        int year = argv[2];
        if(month<1 || month>12){
            printf("%d is not a month", month);
            return 0;
        }
        if(year<1900 || year>9999){
            printf("%d not in range 1900...9999", year);
            return 0;
        }
        calendarMonth(month, year);
        return 0;
    }
    else{
        calendarNow();
        return 0;
    }
    return 0;
}

Are you sure that the warning doesn't say "makes integer from pointer" rather than "makes pointer from integer"? I have no idea why it would say the latter for your code and my compiler at least says the former.

The reason for that warning is that the elements of argv are pointers (char pointers to be specific), not integers and thus assigning one to an integer is likely a bug (and it definitely is in this case).

int year = argv[1];

argv[i] has to be converted to an integer before it can be assigned to int variable. There are several ways to do it, this is probably the simplest, but most error prone. atoi() does not produce an error even if argv[i] does not contain numerical digits.

int year = atoi(argv[1]);

A better solution is to use strtol() because you can detect errors with it. You can also use it to convert digits with different bases, such as hex and octal numbers.

char* endptr = 0;
int year = strtol(argv[i],&endptr,10);
if( *endptr != 0) 
{ // test to see if argv[i] contains only numeric digits
  printf("Error\n");
}

Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.