C++ 如何使用scanf获取指针大小的整数?
我有一些FlexLM的继承代码,它将整数转换为指针,需要在32位和64位机器上工作。使用scanf读取整数值时,正在从参数的argc向程序填充整数 我应该如何可靠地读取argc字符串以获得适合分配给指针的值,从而使其在32位和64位机器上都能工作 目前,代码如下所示:C++ 如何使用scanf获取指针大小的整数?,c++,32bit-64bit,command-line-arguments,scanf,flexlm,C++,32bit 64bit,Command Line Arguments,Scanf,Flexlm,我有一些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 */
// 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);
}
}
[...]
起初,我想我可以使用uintptru\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根据指针大小制作两个不同的版本,因为这在我看来似乎是代码上的一个难看的缺点。32位程序在64位体系结构中运行良好
int address = 0x743358;
int *foo = reinterpret_cast<int*>( address );
int地址=0x743358;
int*foo=reinterpret_cast(地址);
我想这就是你想要的。一个32位的程序在64位的体系结构中可以正常运行
int address = 0x743358;
int *foo = reinterpret_cast<int*>( address );
int地址=0x743358;
int*foo=reinterpret_cast(地址);
我想这就是您要寻找的。C99
inttypes.h
应该定义一个宏SCNuPTR
,它是平台上用于uintptr参数的正确scanf格式说明符
uintptr_t intptr = 0;
sscanf(argv[i], SCNuPTR, &intptr);
C99
inttypes.h
应该定义一个宏SCNuPTR
,它是平台上uintptr参数使用的正确scanf格式说明符
uintptr_t intptr = 0;
sscanf(argv[i], SCNuPTR, &intptr);
它是C++编译器构建的C++应用程序的一部分,虽然这部分非常类似C,它是C++编译器构建的C++应用程序的一部分,虽然这一部分非常类似C。不,我必须构建一个64位应用程序。让我们不依赖于我们答案中的链接,而只使用它们来备份。链接可能会随着时间的推移而过时,在这种情况下,当我单击您的链接时,答案不会向我跳出来。不,我必须构建一个64位应用程序。我们不要依赖答案中的链接,而只将其用于备份。链接可能会随着时间的推移而过时,在这种情况下,答案是当我点击你的链接时不会向我跳出来。