I'm writing a program that converts hexadecimal values to the number digit value, to that end I've made it so the integer function htoi(char s[i]) accepts char arrays.
I plug in a char array using getchar() to allow the user to input characters which then get assigned to an array in my code, and said array is then used as the argument of htoi, however the compiler complains that htoi is using an argument with a pointer without a cast and wants char.
I've tried changing the arrays like char s[] to int s[i], because a char is just a smaller int, right? So, when I change it to int, the compiler then complains that htoi{s[i]) (s[] being an int now) makes a pointer from integer without a cast.
So, I realize that an iterator within an array is nothing but a pointer to an element within the array, okay. Great, so I remove I and. . . It expects an expression before ']' token.
I've exhausted my toolkit.
#include <stdio.h>
#define YES 1
#define NO 0
#define EXIT '~'
int htoi(int s[]);
int main(){
int i, c = 0;
int s[i];
for (i = 0; (c = getchar()!= EXIT); i++){
s[i] = c;
if(c == '\t' || c == '\n' || c == ' '){
htoi(s[]);
}else{
putchar(c);
}
}
}
int htoi(int s[]){
int isHexidecimal;
int hexdigit;
int n;
int i = 0;
if(s[i] == '0'){
i++;
if(s[i] == 'x' || s[i] == 'X')
i++;
}
isHexidecimal = YES;
while(isHexidecimal == YES){
if(s[i] >= '0' && s[i] <= 9){
hexdigit += s[i] - '0';
i++;
}else if(s[i] >= 'a' && s[i] <= 'f'){
hexdigit += s[i] - 'a' + 10;
i++;
}else if(s[i] >= 'A' && s[i] <= 'F'){
hexdigit += s[i] - 'A' + 10;
i++;
}else{
isHexidecimal = NO;
}if(isHexidecimal == YES) {
n = n * 16 + hexdigit;
}else {
n += 0;
}
}
return n;
}
I expect the main method to run the getchar() function which is assigned to variable c, and then assign variable c to the array, then pass said array onto htoi.
htoi would return the hexadecimal to integer value, or '0' if I have not entered a valid hexadecimal value, or returns the hexadecimal value and terminates as a result of the loop not having a valid hexadecimal value entered.