-2

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.

  • `int i, c = 0; int s[i];` Since `i` has no particular value, `int s[i];` tries to declare an array with no particular size. Ouch. – David Schwartz Mar 26 '19 at 23:00
  • OOF. *cough* Thanks, David-San. Alright, but I'm curious. Why does this trick work in Java but not in C? The chaining of assignments? I'm learning C through the C programming Language book 2nd edition, so some things are a little lost on me here. –  Mar 26 '19 at 23:15
  • Where is the chain in "i,c = 0" ? It's not "i = c = 0" as in https://stackoverflow.com/questions/13116833/assigning-a-value-to-a-variable-in-a-chain-in-java ... Only the "int" is kept with the , it doesn't try to assign to a tuple! – B. Go Mar 27 '19 at 00:21
  • So you need to set a max size to s, check it in the for loop (or risk a buffer overflow), and your current code won't work correctly for a second value input after one of your 3 separators. Actually as your string never ends with a \0, I'm not sure it will work even for the first value! – B. Go Mar 27 '19 at 00:25
  • Or any other valid stopping condition, as you're not using a string... Right now you parse uninitialized memory, it could contain anything after your value's chars – B. Go Mar 27 '19 at 00:28
  • Well hang on B. Go, according to the book, the terminating character in every char array is '\0' the escape. (Which might explain why I should use a char array instead of an int. . . .) –  Mar 27 '19 at 00:30
  • It doesn't try to assign to a tuple, in C understood. Is that the same with C++? I'm studying that right after C. –  Mar 27 '19 at 00:31

1 Answers1

1
htoi(s[]);

is invalid, it doesn't mean anything. Just pass the pointer to the array.

htoi(s);

The

int htoi(int s[]);

The array declaration int s[] inside a function parameter list int htoi( <here> ) is interpreted exactly as if it is a pointer to the type int *s

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • He may want to look at [C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3) to figure out where the pointer you are talking about comes from. – David C. Rankin Mar 26 '19 at 22:55
  • I understand. Since I'm bringing up an array with an iterator, it works just like an array with a pointer, and therefore it doesn't work as I had intended. –  Mar 26 '19 at 23:16