Speed up development with full-stack environments for every branch.

Learn More

How to perform typecasting in C++

429 Runs 3392 Views 404 Copies
Saved

Saved

tianxiasoft 13

tianxiasoft
published 4 years ago

/* This is C++ sample code for Typecasting */

#include <iostream>
using namespace std;

int main()
{
  
  // Typecasting method in C language
	char *str="runnable.com";
	int *pi;
	char *pc;
	pi=(int *)str;
	pc=(char *)pi;
	cout << pc;

	// Typecasting static_cast in C++ language
	double d=123.456;
	int i;
	i = static_cast<int>(d);                 // available
	// pi = static_cast<int *>(str);         // error
	pi = (int *)str;                         // available

	// Typecasting reinterpret_cast in C++ language
	pi=reinterpret_cast<int *>(12345678);	// available
	pc=reinterpret_cast<char *>(pi);		// available

	// Typecasting const_cast in C++ language
	char str2[]="string";
	const char *c1=str2;
	char *c2;
	c2=const_cast<char *>(c1);
	c2[0]='a';

	cin.get();
  return 0;
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).