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 Casting a non-`void` pointer to `uintptr_t`

I came across the following code snippet on the SEI CERT C Coding Standard wiki: Compliant Solution Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change i...

1 answer  ·  posted 3d ago by mm1‭  ·  last activity 1d ago by Lundin‭

Question c pointers
#7: Post edited by user avatar mm1‭ · 2025-03-02T23:56:34Z (2 days ago)
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to `intptr_t` or `uintptr_t` and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a `char *` pointer to a `uintptr_t`, as in this compliant solution, is allowed on implementations that support the `uintptr_t` type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to `void` may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to `void` and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to `intptr_t` or `uintptr_t` and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a `char *` pointer to a `uintptr_t`, as in this compliant solution, is allowed on implementations that support the `uintptr_t` type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectively (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to `void` may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to `void` and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#6: Post edited by user avatar mm1‭ · 2025-03-02T08:12:31Z (3 days ago)
Formatting
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to `intptr_t` or `uintptr_t` and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a `char *` pointer to a `uintptr_t`, as in this compliant solution, is allowed on implementations that support the `uintptr_t` type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to `void` may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to `void` and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#5: Post edited by user avatar mm1‭ · 2025-03-02T02:30:10Z (3 days ago)
Formatting.
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#4: Post edited by user avatar mm1‭ · 2025-03-02T01:09:55Z (3 days ago)
  • Casting a non-`void` pointer to `uintptr_t` directly
  • Casting a non-`void` pointer to `uintptr_t`
#3: Post edited by user avatar mm1‭ · 2025-03-02T00:34:41Z (3 days ago)
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#2: Post edited by user avatar mm1‭ · 2025-03-02T00:33:56Z (3 days ago)
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the cast to `void *`.
  • -------------------------------------
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
  • I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
  • > **Compliant Solution**
  • >
  • > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
  • >
  • > ```
  • > #include <stdint.h>
  • >
  • > void f(void) {
  • > char *ptr;
  • > /* ... */
  • > uintptr_t number = (uintptr_t)ptr;
  • > /* ... */
  • > }
  • > ```
  • The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
  • My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).
  • So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the cast to `void *`.
  • -------------------------------------
  • **References:**
  • [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
  • > 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `intptr_t`
  • >
  • > The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
  • >
  • > `uintptr_t`
  • [N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
  • > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • [N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
  • > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#1: Initial revision by user avatar mm1‭ · 2025-03-02T00:32:18Z (3 days ago)
Casting a non-`void` pointer to `uintptr_t` directly
I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):

> **Compliant Solution**
>
> Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
>
> ```
> #include <stdint.h>
>   
> void f(void) {
>  char *ptr;
>  /* ... */
>  uintptr_t number = (uintptr_t)ptr; 
>  /* ... */
> }
> ```

The first two sentences are referring to [N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [&sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.

My understanding is that &sect;7.22.1.4(1) and &sect;6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. &sect;7.22.1.4(1) and &sect;6.3.2.3(1) no longer apply and we have to fall back on [&sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since &sect;6.3.2.3(6) does not make any roundtrip guarantees like those of &sect;7.22.1.4(1) and &sect;6.3.2.3(1).

So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the cast to `void *`.

-------------------------------------

[N3220 &sect;7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):

> 1. The following type designates a signed integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
>
>      `intptr_t`
>
> The following type designates an unsigned integer type with the property that any valid pointer to `void` can be converted to this type, then converted back to pointer to `void`, and the result will compare equal to the original pointer:
>
>      `uintptr_t`

[N3220 &sect;6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):

> 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

[N3220 &sect;6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):

> 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.