简体   繁体   中英

Dereference Operator when using with argv[i]

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (isdigit(*argv[1]))
    {
        int x = atoi(argv[1]);
        printf("%d\n", x);
    }
    return 0;
}

I don't get why asterisk sign need when using isdigit() and why asterisk sign is not needed when using atoi()

argv[1] is a string, and so *argv[1] is the first character in the string. Same thing as argv[1][0] . Calling isdigit(*argv[1]) therefore checks if the first character in the string is a digit.

That test is being used to check if the argument is (probably) a number, after which the entire string is passed into atoi() to get the whole value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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