C++ static_cast function

Among the two types of casting:implicit and explicit,in C++ static_cast can perform the explicit type of casting.This post explains how to use static_cast function to perform all the possible casting from one type to another type.

Link Casting implicit and explicit type

The casting between different data types that will be discussed in this post is shown below.

i)Casting from int to float type
ii)Casting from float to int
iii)Casting from float to char
iv)Casting char to float
v)Casting int to char and vice versa
vi)Casting from char to string
vii)Casting from string to char
viii)Casting from void pointer to int,char,float and string pointer
ix)Casting from int,char,string,float pointer to void pointer

i)Casting from int to float type

It is easy to perform casting from int to float type with static_cast.

int num=90 ;

float result=static_cast<float>(num)/static_cast<float>(80) ; 

Note here the denominator value 80 is a double type,it is converted to float type using the static_cast.

ii)Casting from float to int

Casting from floating point type to integer type can create a dangerous situation for us.Suppose if we cast a floating point number with a fractional value to int type,the fractional value is lost.So it is always advised to minimize such casting in your programs.

loat val=90.78 ;

int product=78 * static_cast<int>(val) ;

cout<< product ;

Check the output yourself.

iii)Casting from float to char

When casting from floating point type to char type if any fraction is found it is neglected and the number is map to ASCII chart and the corresponding character is displayed.

float val=45.56 ;

char c=static_cast<char>(45) ;

cout<< c ;

The output is,

iv)Casting char to float

Casting from char to float type gives the integer value of the character as given in ASCII chart.The code below performs casting from char to float type.

char c=’100′ ;

float f=static_cast<float>(c) ;

cout<< f ;

v)Casting int to char and vice versa

Integer and character type has the same internal format so whether you cast it using static_cast or not ,either way it works fine.

int val=90 , new=static_cast<int>(‘B’) ;

char c=val , cc= static_cast<char>(val) ;

Link : How char and int type has the same format

vi)Casting from char to string

Casting from char to string type using static_cast function is not possible.Though,char type holds a letter and string type can hold a collections of letters it seems these two are treated as a completely different types;not exaggerating even to say int and char type are treated more equally.

char c=’G’;

string s=static_cast<string>(c) ;//error!!
Converting char to string type

Although static_cast does not support casting from char to string type we cannot give up yet,can we? So let us design a simple function that can convert char to string type.Since every char type variable consists of only one character what our function will do is assign that character to C type string and assign the C type string again to C++ type string.The simple function is shown below.

string char_to_string(const char c )
char cstr[2]=”” ; //A C type string
cstr[0]=c ;

return cstr ;

int main( )
char c=’G’ ;

string s=char_to_string(c) ;

cout<< s ;

return 0;

The output is,


Since char variable hold only one letter converting the char type to string type should also consist of only letter.

vii)Casting from string to char

Converting string to char type with static_cast is not possible.Even if it is possible it wouldn’t make any sense.So in this case even if we try to develop a new function to convert string to char type,which letter would we choose from the multiple letters of the string to assign to the char variable;undoubtedly an impossible decision to make.

viii)Casting from void pointer to int,char,float and string pointer

I am sure you know ‘void’simply means “no type”.Since void type is not bound to any type specifically,void pointer can point to any of the built-in type.

Void pointer pointing to the built-in type means casting is involve, fortunately casting void pointer to any of the type falls in the hand of static_cast function.Casting void pointer to int or float or char or string pointer follows the normal rule.A code example is shown below.

int ival=890 , *ipt=&ival ;
char c=’B’ , *cpt=&c ;
float f=89.899 , *fpt=&f ;
string str=”Shoe” , *spt=&str ;

void *vdi =static_cast<void* >(ipt) ,
*vdc=static_cast<void* >(cpt) ,
*vdf=static_cast<void* >(fpt) ,
*vds=static_cast<void* >(spt) ;

void v=static_cast<void>(ival) ;//error!!

Casting a built-in data type variable to void type is not allowed.

ix)Casting from int,char,string,float pointer to void pointer

If any of the data type pointer is casted to void pointer type ,we should cast it back to the original type and assign it to the original type.The static_cast can also handle this operation.Consider the code below.

int *ip=static_cast<int*>(vdi) ;
string *sp=static_cast<string*>(vds) ;

Be careful when casting ‘void’ pointer back to it’s original type,if you get the type wrong,you will most probably get some undefined value when trying to access the value using the pointer which is assigned the void pointer.

float *fp=static_cast<float*>(vdi) ;

cout<< *fp ; //undefined value


Using static_cast to cast string to integer or floating point type is not possible.But there are some other ways to achieved this result.Another post is dedicated for the discussion of this type of casting.The methods discussed uses the C++ ways,C ways and using the Boost library.The link is provided below.

Leave a Reply

Your email address will not be published. Required fields are marked *