Take the 2-minute tour ×

In designing a new programming language, is it safe to assume that a C int and a pointer are the same size on the machine?

share|improve this question

migrated from programmers.stackexchange.com Jan 1 '11 at 16:42


Probably a stack overflow question. –  Orbling Jan 1 '11 at 16:08
possible duplicate of sizeof (int) == sizeof (void*)? –  Amro Jul 26 '13 at 4:12

7 Answers 7

up vote 14 down vote accepted

No. A pointer may be larger or smaller than an integer in size. If you need to pass a pointer as an integer for some reason (like performing integer, rather than pointer, arithmetic), they are guaranteed to fit into an intptr_t.

They are not guaranteed to fit into a size_t as suggested in another answer, but in practice it is unlikely that they won't, since the largest addressable size is usually equal to the largest addressable address.

share|improve this answer

No, not at all. Many compilers do not have them as the same size.

share|improve this answer

No, especially in 64 bit environments:

LP64 This covers *nix environments but the same is true in windows for LLP64.

share|improve this answer

I think you mean size of data types as defined by platform not C lang. To best of my knowledge C doesn't define any specific size for the data types. The answer to your question is you can't assume this, for example On win32 sizeof(int) == sizeof(pointer) == 4 bytes however on win64 sizeof(int) == 4 and sizeof(pointer) == 8

share|improve this answer
The C language defines several minimum data sizes (e.g. size_t must be at least 16 bits), relational sizes (long can't be shorter than short), as well as some fixed-size types (uint32_t is exactly 32 bits). –  user79758 Jan 1 '11 at 17:20
@Joe you are right, I should have restricted my answer to relative size of int and pointer. –  Gaurav Jan 1 '11 at 20:23
@Joe: It seems that uint32_t first showed up in the C standard in C99 (see en.wikipedia.org/wiki/Stdint.h) because that's when stdint.h became part of the standard. (I don't know if implementations would sometimes provide it before C99.) –  compman Jun 28 '11 at 18:39
@compman: Before, most implementations typically would define integer types that had fixed sizes; but, since that's not portable, lots of projects have their own ways of defining these, typically using autoconf to set them to the correct equivalents to get the size right for the current compiler. You can compile and run a small C program to tell you the sizeof(short), sizeof(int), etc., then use the result to write a header file that has the appropriate typedefs for int16, int32, etc. –  Nate C-K Jun 23 '12 at 15:18

no, but a pointer should be the same size as a intptr_t.

share|improve this answer
It seems that sizeof(int) != sizeof(int *) with the machine and compiler that I'm using at the moment... so if something similar is safe, it doesn't involve int. –  compman Jan 1 '11 at 16:04
@user9521: As Dan says, always use the size_t types. The main issue with converting code to x64 is the prevalence of people assuming the int size is the same, that practice was deprecated many years ago. –  Orbling Jan 1 '11 at 16:10
-1 - De facto you are likely correct, but de jure you are wrong. On segmented (and other kinds of pathological) memory architectures, size_t may be smaller than intptr_t. stackoverflow.com/questions/1464174/size-t-vs-intptr-t. –  user79758 Jan 1 '11 at 17:23
@Joe, you learn a new thing every day. –  dan_waterworth Jan 1 '11 at 17:57
Technically, your answer is still wrong even after the edit. An intptr_t can safely store a pointer, meaning it is at least as large. When sizeof(int) == sizeof(void*), then usually that is also sizeof(intptr_t). But I do not believe there is a requirement that sizeof(int) <= sizeof(void*), nor a requirement that sizeof(intptr_t) = max(sizeof(void*), sizeof(int)). –  user79758 Feb 14 '11 at 20:20

No; on my MacOS X 10.6.5. machine, an int is 32 bits and a pointer is 64 bits by default.

If you need an integer that's the right size to hold a pointer too, use #include <inttypes.h> (or <stdint.h>) and uintptr_t - assuming you have C99 support, or can simulate it.

share|improve this answer

I believe the Linux Kernel passes pointers as unsigned long's. They are guaranteed to be at least the same size as a pointer :)

share|improve this answer
No. Linux guarantees they are the same size. The C language makes no promises whatsoever. –  R.. Jan 2 '11 at 3:21

Your Answer


