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
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: Post edited
- 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
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.