Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of Pointer Variables

Considering pch, pshort, pdouble declared as pointers to char ,short int ,double respectively, what would be the arrangement if the three variables were arranged according to their size ?

like image 368
TanayJha Avatar asked May 11 '26 19:05

TanayJha


2 Answers

The facetious answer is you don't know. char, short, and double could all be the same size, and char*, short*, and double* could all be different sizes!


sizeof(char) is 1 by the standard. You can't have anything smaller than that, so it makes sense to put char first.

But short int could be the same size as a long int: the standard only specifies minimum ranges. And either could be larger than a double.

Normally a double weighs in at 64 bit, and a short 16 or 32 bits.

The parsimonious answer is char, short, double.


As for pointers, the standard allows sizeof(char*), sizeof(short*), and sizeof(double*) to all differ.

like image 82
Bathsheba Avatar answered May 13 '26 10:05

Bathsheba


Pointers to different types may have different sizes, although on most modern platforms they are all the same size (32 bits on x86, 64 bits on x86_64).

The requirements1 are:

  • Pointers to char and pointers to void have the same size and representation;
  • Pointers to struct types all have the same size and representation;
  • Pointers to union types all have the same size and representation;
  • Pointers to qualified and unqualified versions of compatible types have the same size and representation (i.e., sizeof (int*) == sizeof (const int *))
  • Pointers to all other types may have different sizes.


  1. C 2011 Online Draft, section 6.2.5, para 28.

like image 26
John Bode Avatar answered May 13 '26 08:05

John Bode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!