2

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

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
ryanxu
  • 101
  • 1
  • 3
  • 8

3 Answers3

5
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.

Community
  • 1
  • 1
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
1

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

char y= *x;
short b= atoi(a);
MSN
  • 53,214
  • 7
  • 75
  • 105
0
  • 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
  • 74,049
  • 16
  • 131
  • 175