What is intptr_t,is it a type for integer or pointer?

Active3 hr before
Viewed126 times

6 Answers


Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. 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 is allowed on implementations that support the uintptr_t., Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português

It is a signed integer type that guaranteed to can hold a void* type.

void *
load more v

Do not convert a pointer type to an integer type if the result cannot be represented in the integer type. (See undefined behavior 24.),A null pointer can be converted to an integer; it takes on the value 0.,A conversion should not be performed between a pointer to object type and an integer type other than 'uintptr_t' or 'intptr_t',Do not convert an integer type to a pointer type if the resulting pointer is incorrectly aligned, does not point to an entity of the referenced type, or is a trap representation.

void f(void) {
   char * ptr;
   /* ... */
   unsigned int number = (unsigned int) ptr;
   /* ... */
load more v

Used for storing pointer addresses,The variable pi is of type pointer to an integer and num is of type integer. The error message is saying we cannot convert an integer to a pointer to the data type integer.,If we try to assign the address of an integer to a pointer of type uintptr_t as follows, we will get a syntax error:,The size_t is a data type used for sizes and is discussed in the section Predefined Pointer-Related Types.

    char * names[] = {
    printf("%c\n", *( * (names + 1) + 2));
    printf("%c\n", names[1][2]);
load more v

2.2 Signed integers : minimum value,Each of the SCN macros listed in here is defined if and only if the implementation defines the corresponding typedef name and has a suitable fscanf length modifier for the type. ,2.3 Signed integers : maximum value,2.5 Unsigned integers : maximum value

#include <stdint.h>
   UINT64_C(0x123) // might expand to 0x123ULL or 0x123UL
load more v

intptr_t is a new invention, created after 64-bit and even 128-bit memory addresses were imagined.,   c  -  types  -  casting  -  memory-management  -  intptr ,After some googling and binging (is the latter a word yet?), I saw some people recommend using intptr_t:,It is also no good for arithmetic / accessing individual bytes. Cast to (unsigned char*) instead.

At first I did something like this:

char myChar, * pChar;
float myFloat, * pFloat;

pChar = & myChar;
pFloat = & myFloat;

printf("pChar:  %d\n", (int) pChar);
printf("pFloat: %d\n", (int) pFloat);


printf("and then after incrementing,:\n\n");
printf("pChar:  %d\n", (int) pChar);
printf("pFloat:    %d\n", (int) pFloat);

After some googling and binging (is the latter a word yet?), I saw some people recommend using intptr_t:

#include <stdint.h>


printf("pChar:  %ld\n", (intptr_t) pChar);
printf("pFloat: %ld\n", (intptr_t) pFloat);

which indeed resolves the errors. So, I thought, from now on, I should use intptr_t for typecasting pointers... But then after some fidgeting, I found that I could solve the problem by just replacing int with long int:

printf("pChar:  %ld\n", (long int) pChar);
printf("pFloat: %ld\n", (long int) pFloat);
load more v

ptrdiff_t is a signed integer type used to represent the difference between pointers. It is guaranteed to be valid only against pointers of the same type; subtraction of pointers consisting of different types is implementation-defined. ,1.3 Size and pointer difference types,Pointer integer types that are guaranteed to be able to hold a pointer. Included only if it is available in the implementation.,1.4 Interface to the properties of the basic types 1.4.1 Properties of integer types 1.4.2 Properties of floating-point types

unsigned char b = 256;

if (b) {
   /* do something */
load more v

Other "undefined-undefined" queries related to "What is intptr_t,is it a type for integer or pointer?"