我正在尝试将 optarg 值分配给 int,但编译器给了我以下警告:
warning: assignment makes integer from pointer without a cast [enabled by default]
我尝试在分配之前将 optarg 强制转换为 int
n = (int) optarg;
但仍然收到警告:
warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
我不确定在我可以简单地将 optarg 分配给一个整数之前需要做什么,然后打印它(现在)。
int main (int argc, char *argv[])
{
char c;
int n;
while ((c = getopt(argc, argv, "m:")) != -1) {
switch (c) {
case 'm':
n = optarg;
break;
}
}
printf("%d\n", n);
return 0;
}