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

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

Void pointers are compatible with every other object pointer type and as mentioned in another answer, 7.21.6/10 speaks of the type of the pointed at object, not the type of the pointer. This is con...

posted 3y ago by Lundin‭  ·  edited 3y ago by Lundin‭

Answer
#3: Post edited by user avatar Lundin‭ · 2022-02-17T07:18:31Z (almost 3 years ago)
  • Void pointers are compatible with every other object pointer type and as mentioned in another answer, 7.21.6/10 speaks of the type of the pointed at object, not the type of the pointer. This is consistent with the rules of _effective type_/pointer aliasing (6.5/6), which have to be applied as well, since the passed pointer does not necessarily point to a chunk of memory with an object of a declared type (could as well be a void pointer returned from `malloc`). `scanf` has to be regarded as a "lvalue" access following the rules of effective type. Examples:
  • ```c
  • float f;
  • scanf("%d", &f); // undefined behavior, 7.21.6/10 and 6.5/7
  • void* v = &f;
  • scanf("%f", v); // well-defined behavior, object f has type float
  • void* p = malloc(n);
  • scanf("%d", p); // well-defined, *p is now to be regarded as effective type int
  • ```
  • For `scanf` to make any sense, it will have to internally cast the passed pointer to a pointer to the type of the specified conversion specifier. Any type information that the passed pointers might have had is lost through the varadic function/`va_list` parameter passing anyway.
  • Notably, there's a whole lot of scenarios where `scanf` can go wrong, so it is mostly to be used for debugging purposes - it is not a function recommended to be used in any professional release. If your program for some reason must use console input, then use `fgets` as far as possible.
  • Void pointers are compatible with every other object pointer type and as mentioned in another answer, 7.21.6/10 speaks of the type of the pointed at object, not the type of the pointer. This is consistent with the rules of _effective type_/pointer aliasing (6.5/6), which have to be applied as well, since the passed pointer does not necessarily point to a chunk of memory with an object of a declared type (could as well be a void pointer returned from `malloc`). `scanf` has to be regarded as a "lvalue" access following the rules of effective type. Examples:
  • ```c
  • float f;
  • scanf("%d", &f); // undefined behavior, 7.21.6/10 and 6.5/7
  • void* v = &f;
  • scanf("%f", v); // well-defined behavior, object f has declared and effective type float
  • void* p = malloc(n); // location pointed at by p has no declared type
  • scanf("%d", p); // well-defined, *p is now to be regarded as effective type int
  • ```
  • For `scanf` to make any sense, it will have to internally cast the passed pointer to a pointer to the type of the specified conversion specifier. Any type information that the passed pointers might have had is lost through the varadic function/`va_list` parameter passing anyway.
  • Notably, there's a whole lot of scenarios where `scanf` can go wrong, so it is mostly to be used for debugging purposes - it is not a function recommended to be used in any professional release. If your program for some reason must use console input, then use `fgets` as far as possible.
#2: Post edited by user avatar Lundin‭ · 2022-02-17T07:17:04Z (almost 3 years ago)
  • Void pointers are compatible with every other pointer type and as mentioned in another answer, 7.21.6/10 speaks of the type of the pointed at object, not the type of the pointer. This is consistent with the rules of _effective type_/pointer aliasing (6.5/6), which have to be applied as well, since the passed pointer does not necessarily point to a chunk of memory with an object of a declared type (could as well be a void pointer returned from `malloc`). `scanf` has to be regarded as a "lvalue" access following the rules of effective type. Examples:
  • ```c
  • float f;
  • scanf("%d", &f); // undefined behavior, 7.21.6/10 and 6.5/7
  • void* v = &f;
  • scanf("%f", v); // well-defined behavior, object f has type float
  • void* p = malloc(n);
  • scanf("%d", p); // well-defined, *p is now to be regarded as effective type int
  • ```
  • For `scanf` to make any sense, it will have to internally cast the passed pointer to a pointer to the type of the specified conversion specifier. Any type information that the passed pointers might have had is lost through the varadic function/`va_list` parameter passing anyway.
  • Notably, there's a whole lot of scenarios where `scanf` can go wrong, so it is mostly to be used for debugging purposes - it is not a function recommended to be used in any professional release. If your program for some reason must use console input, then use `fgets` as far as possible.
  • Void pointers are compatible with every other object pointer type and as mentioned in another answer, 7.21.6/10 speaks of the type of the pointed at object, not the type of the pointer. This is consistent with the rules of _effective type_/pointer aliasing (6.5/6), which have to be applied as well, since the passed pointer does not necessarily point to a chunk of memory with an object of a declared type (could as well be a void pointer returned from `malloc`). `scanf` has to be regarded as a "lvalue" access following the rules of effective type. Examples:
  • ```c
  • float f;
  • scanf("%d", &f); // undefined behavior, 7.21.6/10 and 6.5/7
  • void* v = &f;
  • scanf("%f", v); // well-defined behavior, object f has type float
  • void* p = malloc(n);
  • scanf("%d", p); // well-defined, *p is now to be regarded as effective type int
  • ```
  • For `scanf` to make any sense, it will have to internally cast the passed pointer to a pointer to the type of the specified conversion specifier. Any type information that the passed pointers might have had is lost through the varadic function/`va_list` parameter passing anyway.
  • Notably, there's a whole lot of scenarios where `scanf` can go wrong, so it is mostly to be used for debugging purposes - it is not a function recommended to be used in any professional release. If your program for some reason must use console input, then use `fgets` as far as possible.
#1: Initial revision by user avatar Lundin‭ · 2022-02-17T07:16:41Z (almost 3 years ago)
Void pointers are compatible with every other pointer type and as mentioned in another answer, 7.21.6/10 speaks of the type of the pointed at object, not the type of the pointer. This is consistent with the rules of _effective type_/pointer aliasing (6.5/6), which have to be applied as well, since the passed pointer does not necessarily point to a chunk of memory with an object of a declared type (could as well be a void pointer returned from `malloc`). `scanf` has to be regarded as a "lvalue" access following the rules of effective type. Examples:


```c
float f;
scanf("%d", &f); // undefined behavior, 7.21.6/10 and 6.5/7
void* v = &f;
scanf("%f", v); // well-defined behavior, object f has type float

void* p = malloc(n);
scanf("%d", p); // well-defined, *p is now to be regarded as effective type int
```

For `scanf` to make any sense, it will have to internally cast the passed pointer to a pointer to the type of the specified conversion specifier. Any type information that the passed pointers might have had is lost through the varadic function/`va_list` parameter passing anyway.

Notably, there's a whole lot of scenarios where `scanf` can go wrong, so it is mostly to be used for debugging purposes - it is not a function recommended to be used in any professional release. If your program for some reason must use console input, then use `fgets` as far as possible.