tags:

views:

63

answers:

3

char * x="a"; how would i convert it to char y='a';

also if i have a short char * a="100" how can i convert it to short b=100

thanks

+3  A: 
char * x = "a";
char y = *x; //or x[0]


char * a = "100";
short b = atoi(a);

Note that assigning return value of atoi to a short might lead to overflow.

Also read why strtol is preferred over atoi for string to number conversions.

Amarghosh
A: 
  • A char * can be used as an array of chars. To get the first letter, use char y = x[0]
  • A string can be converted to a number using the function atoi
Sjoerd
+1  A: 

Assuming that's all you wanted to do and didn't care about error checking:

char y= *x;
short b= atoi(a);
MSN