sizeOf and typedef Operators in C++

In this tutorial we will cover the usage of sizeOf and typedef operators in C++.

sizeOf is also an operator not a function, it is used to get information about the amount of memory allocated for data types & Objects. It can be used to get size of user defined data types too.

sizeOf operator can be used with and without parentheses. If you apply it to a variable you can use it without parentheses.

cout << sizeOf(double);   //Will print size of double
int x = 2;
int i = sizeOf x;

typedef Operator in C++

typedef is a keyword used in C language to assign alternative names to existing types. Its mostly used with user defined data types, when names of data types get slightly complicated. Following is the general syntax for using typedef,

typedef existing_name alias_name

Lets take an example and see how typedef actually works.

typedef unsigned long ulong;

The above statement define a term ulong for an unsigned long type. Now this ulong identifier can be used to define unsigned long type variables.

ulong i, j;

typedef and Pointers

typedef can be used to give an alias name to pointers also. Here we have a case in which use of typedef is beneficial during pointer declaration.

In Pointers * binds to the right and not the left.

int* x, y ;

By this declaration statement, we are actually declaring x as a pointer of type int, whereas y will be declared as a plain integer.

typedef int* IntPtr ;
IntPtr x, y, z;

But if we use typedef like in above example, we can declare any number of pointers in a single statement.