Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get pointer sized integer using scanf?
    text
    copied!<p>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.</p> <p>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?</p> <p>Presently the code looks something like this:</p> <pre><code>// 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 &gt;= argc) { break; } sscanf(argv[i], "%d", &amp;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); } } [...] </code></pre> <p>At first I was thinking I could use <code>uintptr_t</code>, but how would I get <code>scanf</code> to know the size accordingly? Perhaps I should just read it as a pointer value using <code>%p</code>, but the man page gives me doubts about it working reliably:</p> <pre><code> 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. </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload