Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all data pointers the same size in one platform for all data types?

Tags:

c

types

pointers

Are char*, int*, long* or even long long* of same size (on a given platform)?

like image 201
c-stranger Avatar asked Aug 06 '09 20:08

c-stranger


People also ask

Does pointer size depend on data type?

The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes.

Why size of pointer variable is same for each data type?

Thus, pointers to all types of data occupy the same size of memory because the value of a pointer is the memory address – an unsigned integer. However, the variables to which the pointers point to, of course, occupy different sizes of memory blocks according to their types.

How pointers are different from other data types?

Pointer data type is a special kind of variable which are meant to store addresses only, instead of values(integer, float, double, char, etc). It knows how many bytes the data is stored in. When we increment a pointer, we increase the pointer by the size of the data type to which it points.

What are the size of pointers?

On 32-bit machine sizeof pointer is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte.


1 Answers

They're not guaranteed to be the same size, although on the platforms I have experience with they usually are.

C 2011 online draft:

6.2.5 Types
...
28     A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
48) The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
like image 122
John Bode Avatar answered Sep 24 '22 10:09

John Bode