我有一些遗传代码的FlexLM是一个整数转换成需要两个32位和64位计算机上工作的指针。 整数正在从自变量使用的scanf读取整数值的程序的argc填充。
我应该如何可靠地读取的argc字符串来获取适合使其作品在两个32位和64位机分配给一个指针的值?
目前,该代码看起来是这样的:
// FlexLM includes this:
typedef char * LM_A_VAL_TYPE; /* so that it will be big enough for */
/* any data type on any system */
// My main() includes this:
[...]
if (!strcmp(argv[i], "-maxlen")) {
int max = 0;
i++;
if (i >= argc) {
break;
}
sscanf(argv[i], "%d", &max);
if (!max) {
fprintf(stderr, "Error: -maxlen %s Invalid line length\n", argv[i]);
} else {
lc_set_attr(lm_job, LM_A_MAX_LICENSE_LEN, (LM_A_VAL_TYPE)max);
}
}
[...]
起初,我想我可以使用uintptr_t
,但我怎么会得到scanf
相应地知道大小? 也许我应该只用它读成一个指针值%p
,但该男子页让我怀疑它可靠地工作:
p Matches an implementation-defined set of sequences, which shall be the
same as the set of sequences that is produced by the %p conversion
specification of the corresponding fprintf() functions. The
application shall ensure that the corresponding argument is a pointer
to a pointer to void. The interpretation of the input item is
implementation-defined. If the input item is a value converted earlier
during the same program execution, the pointer that results shall
compare equal to that value; otherwise, the behavior of the %p
conversion specification is undefined.
我宁可不使用#ifdef来进行基于指针大小两个单独的版本为好像对代码给我一个丑陋的疣。