How to get pointer sized integer using scanf?

旧巷老猫 提交于 2019-12-13 02:27:09

问题


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.


回答1:


C99 inttypes.h is supposed to define a macro SCNuPTR which is the correct scanf format specifier to use for uintptr_t arguments on your platform.

uintptr_t intptr = 0;
sscanf(argv[i], SCNuPTR, &intptr);



回答2:


A 32bit program will run just fine in a 64bit architecture.

int address = 0x743358;
int *foo = reinterpret_cast<int*>( address );

I guess this is what you're looking for.



来源:https://stackoverflow.com/questions/12228227/how-to-get-pointer-sized-integer-using-scanf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!