0

Can someone please help me on how to specify the width for %p format specifier, for let's say sscanf()?

I know it is unsafe to use sscanf without specifying the width for the sub-string in the format specifier. This was easy for me till I encountered sscanf() with %p specifier.

There already was a question which was primarily on printing a value with %p in printf, but that question was mostly revolving on what possibly is the full width of a %p formatted string. This question is mainly on how to safely use sscanf() with %p specifier, for which I need to specify the full width of the sub-string.

Thanks in advance.

Mariners
  • 499
  • 8
  • 18
  • Possible duplicate of [Why doesn't %p show the full width of pointer?](https://stackoverflow.com/questions/41639002/why-doesnt-p-show-the-full-width-of-pointer) – Achal Aug 05 '19 at 07:47
  • ***Why*** do you need to read a *pointer* with `sscanf`? What is the *real* problem you have and try to solve? – Some programmer dude Aug 05 '19 at 07:47
  • @Achal: Well not so. That is mostly about what is the actual width of a %p formatted string while printing. I am asking on how to specify the bounds of a %p formatted sub-string while using it with sscanf() or scanf(). – Mariners Aug 05 '19 at 07:54
  • Possible duplicate of "how to shoot yourself in the foot safely"? – Antti Haapala -- Слава Україні Aug 05 '19 at 07:55
  • @Someprogrammerdude: Well I don't. It's just that I am cleaning up part of a legacy code-base, where it is used. I am trying to undo it. But before looking for a completely different alternative for sscanf(), I am a bit curious to find a safe way to use sscanf() in these scenarios. – Mariners Aug 05 '19 at 07:55
  • @AnttiHaapala: In that case, do you know how to do that? Is anything wrong in knowing how to do it? It's a different matter whether I'd really do it or not. – Mariners Aug 05 '19 at 07:57
  • The safety aspect is applicable only to string conversions. There are other reasons to specify a field-width but it does not make sense for pointers. If you're scanning pointers from a stream **without delimiters**... – Antti Haapala -- Слава Україні Aug 05 '19 at 07:58
  • You can do `%16p`. Then if you have a pointer that is 17 characters wide then it is not being read. It is not helpful at all. The question does not make sense. – Antti Haapala -- Слава Україні Aug 05 '19 at 08:00
  • 1
    Parsing pointers from strings generally makes no sense, unless the strings (and the contained pointers) were created by the exact same running process. Otherwise the pointer is worthless, and you can't dereference it. And besides that, a pointer is a pointer, its size is fixed. The number of digits only matter if the pointer string was created on a different system (32 bits versus 64 bits), but as mentioned, then you can't use the pointer anyway. – Some programmer dude Aug 05 '19 at 08:01

0 Answers0