Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

80%
+6 −0
Q&A Is it OK to use scanf with a void pointer?

I've created a function that calls scanf passing a void pointer as argument: void read(const char *format, void *p) { scanf(format, p); } And tested it with different types: int n; read...

2 answers  ·  posted 2y ago by hkotsubo‭  ·  last activity 2y ago by Lundin‭

Question c pointers scanf
#2: Post edited by user avatar hkotsubo‭ · 2022-02-16T14:52:20Z (about 2 years ago)
  • I've created a function that calls `scanf` passing a void pointer as argument:
  • ```c
  • void read(const char *format, void *p) {
  • scanf(format, p);
  • }
  • ```
  • And tested it with different types:
  • ```c
  • int n;
  • read("%d", &n);
  • printf("read int: %d\n", n);
  • float f;
  • read("%f", &f);
  • printf("read float: %f\n", f);
  • char s[100];
  • read("%s", s);
  • printf("read string: %s\n", s);
  • ```
  • I've made these tests in Ubuntu 20.04.3, with clang 10.0.0-4ubuntu1 and gcc 9.3.0. Both were compiled with the options `-std=c11 -pedantic-errors -Wall -Wextra -Werror`, without any errors/warnings, and both worked fine (all data were correctly read and printed).
  • But "it worked" doesn't necessarily mean it's correct (specially in C), hence the question: is `scanf` supposed to work like that (receiving a void pointer and correctly assigning the data to it, according to the format specifier), or is it a big coincidence and should be avoided? Are there any cases that could fail (excluding the obvious ones where the format doesn't match the second argument)?
  • ---
  • PS: The point here is **not** to discuss the merits of the `read` function (if it's useless, or should check the return value of `scanf`, or should use `va_list`/`vfscanf` instead, etc). I'm just interested in knowing if this behaviour of `scanf` (accepting a void pointer and correctly "guessing" its type based on the specifier) is expected (defined by the standard, for instance). In case it's not, an explanation about why it worked is also appreciated.
  • I've created a function that calls `scanf` passing a void pointer as argument:
  • ```c
  • void read(const char *format, void *p) {
  • scanf(format, p);
  • }
  • ```
  • And tested it with different types:
  • ```c
  • int n;
  • read("%d", &n);
  • printf("read int: %d\n", n);
  • float f;
  • read("%f", &f);
  • printf("read float: %f\n", f);
  • char s[100];
  • read("%s", s);
  • printf("read string: %s\n", s);
  • ```
  • I've made these tests in Ubuntu 20.04.3, with clang 10.0.0-4ubuntu1 and gcc 9.3.0. Both were compiled with the options `-std=c11 -pedantic-errors -Wall -Wextra -Werror`, without any errors/warnings, and both worked fine (all data were correctly read and printed).
  • But "it worked" doesn't necessarily mean it's correct (specially in C), hence the question: is `scanf` supposed to work like that (receiving a void pointer and correctly assigning the data to it, according to the format specifier), or is it a big coincidence and should be avoided? Are there any cases that could fail (excluding the obvious ones, such as the format doesn't match the second argument)?
  • ---
  • PS: The point here is **not** to discuss the merits of the `read` function (if it's useless, or should check the return value of `scanf`, or should use `va_list`/`vfscanf` instead, etc). I'm just interested in knowing if this behaviour of `scanf` (accepting a void pointer and correctly "guessing" its type based on the specifier) is expected (defined by the standard, for instance). In case it's not, an explanation about why it worked is also appreciated.
#1: Initial revision by user avatar hkotsubo‭ · 2022-02-16T14:51:07Z (about 2 years ago)
Is it OK to use scanf with a void pointer?
I've created a function that calls `scanf` passing a void pointer as argument:

```c
void read(const char *format, void *p) {
    scanf(format, p);
}
```

And tested it with different types:

```c
int n;
read("%d", &n);
printf("read int: %d\n", n);

float f;
read("%f", &f);
printf("read float: %f\n", f);

char s[100];
read("%s", s);
printf("read string: %s\n", s);
```

I've made these tests in Ubuntu 20.04.3, with clang 10.0.0-4ubuntu1 and gcc 9.3.0. Both were compiled with the options `-std=c11 -pedantic-errors -Wall -Wextra -Werror`, without any errors/warnings, and both worked fine (all data were correctly read and printed).

But "it worked" doesn't necessarily mean it's correct (specially in C), hence the question: is `scanf` supposed to work like that (receiving a void pointer and correctly assigning the data to it, according to the format specifier), or is it a big coincidence and should be avoided? Are there any cases that could fail (excluding the obvious ones where the format doesn't match the second argument)?


---

PS: The point here is **not** to discuss the merits of the `read` function (if it's useless, or should check the return value of `scanf`, or should use `va_list`/`vfscanf` instead, etc). I'm just interested in knowing if this behaviour of `scanf` (accepting a void pointer and correctly "guessing" its type based on the specifier) is expected (defined by the standard, for instance). In case it's not, an explanation about why it worked is also appreciated.