Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I convert char a[0] into int b[0] where b is a empty dynamically allocated int array

I have tried

char a[] = "4x^0";
int *b;
b = new int[10];
char temp = a[0]; 
int temp2 = temp - 0;
b[0] = temp2;

I want 4 but it gives me ascii value 52

Also doing

a[0] = atoi(temp);

gives me error: invalid conversion from ‘char’ to ‘const char*’ initializing argument 1 of ‘int atoi(const char*)’

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
316 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to do:

int temp2 = temp - '0';

instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...