I have some inherited code for FlexLM that is converting an integer to a pointer that needs to work on both 32bit and 64bit machines. The integer is being filled in from argc of the arguments to the program using scanf to read the integer value.
How should I reliably read the argc string to get a value suitable for assigning to a pointer so that it works on both 32bit and 64bit machines?
Presently the code looks something like this:
// 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);
}
}
[...]
At first I was thinking I could use uintptr_t
, but how would I get scanf
to know the size accordingly? Perhaps I should just read it as a pointer value using %p
, but the man page gives me doubts about it working reliably:
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.
I'd rather not use #ifdef to make two separate versions based on pointer size as that seems like an ugly wart on the code to me.
C99
inttypes.h
is supposed to define a macroSCNuPTR
which is the correct scanf format specifier to use for uintptr_t arguments on your platform.