I have problem with giving new char valure to array. I don’t know why I get sign “<” even when n is 12? My program should change expression int char* tab = "93+" to one value in this case 12.
char* tab = "93+";
int b = sizeof (tab);
char* tmp = new char[b] ;
tmp [b-1] = '\0';
if(isdigit(tab[i]) && isdigit(tab[i+1]) ){
int n;
if(tab[i+2]=='+' || tab[i+2]=='-' || tab[i+2]=='*'){
switch(tab[i+2]){
case '+':
n = (tab[i]-'0') + (tab[i+1]-'0');
break;
case '-':
n = (tab[i]-'0') - (tab[i+1]-'0');
break;
case '*':
n = (tab[i]-'0') * (tab[i+1]-'0');
break;
}
tmp[i] = n+'0'; // I should have 12 but I get <
}
else if (tab[i+2]!='+' || tab[i+2]!='-' || tab[i+2]!='*'){
goto LAB;
}
}
The problem is in this line:
n is 12, but 12 + ‘0’ != ’12’, since ’12’ isn’t a character. You’re putting into tmp[i] the char whose ascii value is 12 more than ‘0’, which is ‘<‘.
I believe declaring (and treating) tmp as an int would be better for your purposes.
Also note that
sizeof (tab)is the same assizeof (char *), and notsizeof ("93+"), so you’re likely to always getb==4(in 32-bit machines).