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
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...
Answer
#3: Post edited
- 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 floatvoid* 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
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
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.