无效的转换,从"焦炭"到"字符*" [英] Invalid conversion from "char" to "char *"

查看:321
本文介绍了无效的转换,从"焦炭"到"字符*"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是转换的罗马数字(例如Ⅵ)为十进制的程序。
该算法是写识别输入串中的每个元素,并在小数返回对应的值的函数。

我们得到了字符Roman_num [20]
对于每一个元素,总和+ =值(Roman_num [I]) .The函数原型为 int值(字符数字) 。它导致了'的无效从焦炭转化为char *'错误。

然而,通过每个元素的地址&安培时,一个[I] 的功能和改变原型 int值(字符*位) ,它不会重复这个错误,但导致这种开关的情况下另一个错误(在函数内):开关(*位)数字的错误不能出现在一个恒定的-EX pression

我的问题是我没说清楚:在这个例子中,我们有只地址传递给funcion?如果我们想要传递的价值,一个字符值,那么如何?最后但并非最不重要的,*数字实际上是一个字符,那么为什么就不能出现在一个恒定的前pression在情况下()

我将是你的帮助表示感谢。在另一方面,可以请你给我推荐一些书籍在C深刻理解?现在,我使用的 C:如何编程的书,我几乎不知道如何变量,函数在里面工作有了更深的了解。

这是code:

  int值(字符*位);
INT主要(无效)
{
    所以char a [100];
    INT长,我,总和= 0;
    的printf(请输入您的罗马数字:);
    fflush(标准输入);
    得到(一);
    的printf(你输入\\ n此罗马数字为%s,一);
    长度= strlen的(一);
    对于(i = 0; I<长度;我+ +)
    {
        总和+ =值(安培; A []);
    }
    的printf(\\ n然后是数:%d,总和);
    残培();
    返回0;
}
int值(字符*位)
{
    INT NUM;
    案例(*位数){
        案例'M':
            NUM = 1000;
            打破;
        案D:
            NUM = 500;
            打破;
        情况下C:
            NUM = 100;
            打破;
        案例'L':
            NUM = 50;
            打破;
        情况下'X':
            NUM = 10;
            打破;
        案例'V':
            NUM = 5;
            打破;
        案例'我':
            NUM = 1;
            打破;
    }
    返回NUM;
}


解决方案

既然您不会发表code,这里有些code为获得罗马数字总是增大。减法部分是留给你要弄清楚。我只发布这个,因为它听起来像你自我教自己这是comendable:

 
INT romanToValue(常量字符C)
{
    开关(C)//只有大写为小写意味着不同的东西的作品。
    {
      案例'我':1的回报;
      案例'V':回归5;
      情况下'X':10返回;
      案例'L':回归50;
      情况下C:返回100;
      案D:返回500;
      案M:回归1000;
      默认:输出(字符串%C \\ n坏值,C);打破;
    }    返回0;
}INT romanToInt(为const char *海峡)
{
  int值= 0;
  INT I;  对于(i = 0; STR [I];我++)//危险的方式做字符串,但运作一个C的例子。
  {
    值+ = romanToValue(STR [I]);
  }  返回值;
}INT主要(无效)
{
  为const char CSTR [] =八;  的printf(价值数:%d \\ n,romanToInt(CSTR));
  返回0;
}

注意语句正在关闭字符开关。

This is the program to convert a Roman number (for example VI) to a decimal. The algorithm is writing a function that recognize each element of the input string and return the corresponding value in decimal.

We got char Roman_num[20] For each element, sum+=value(Roman_num[i]).The function prototype is int value (char digit).It results in the 'Invalid conversion from char to char *' error.

However, when passing each element's address &a[i] to the function and changing the prototype to int value (char *digit), it doesn't repeat this error but leads to another error in this switch-case (inside the function) : switch (*digit) gives an error of 'digit' cannot appear in a constant-expression

My question is I was not clear that: in this example, do we have to pass only the address to the funcion? If we want to pass the value, a single character value, then how? Last but not least, *digit is actually a single character, then why it cannot appear in a constant-expression in case()?

I will be grateful for your help. On the other hand, can you please recommend me some books for deep understanding in C? I'm now using C : How To Program book, and I hardly know about how the variables, functions working on the inside for a deeper understanding.

This is the code:

int value (char *digit);
int main (void)
{
    char a[100];
    int length,i,sum=0;
    printf("Enter your Roman number: ");
    fflush(stdin);
    gets(a);
    printf("\nThe Roman number that you have entered is %s",a);
    length=strlen(a);
    for (i=0;i<length;i++)
    {
        sum+=value(&a[i]);
    }
    printf("\nthen it is: %d",sum);
    getch();
    return 0;
}
int value (char *digit)
{
    int num;
    case ( *digit ){
        case 'M':
            num=1000;
            break;
        case 'D':
            num=500;
            break;
        case 'C':
            num=100;
            break;
        case 'L':
            num=50;
            break;
        case 'X':
            num=10;
            break;
        case 'V':
            num=5;
            break;
        case 'I':
            num=1;
            break;
    }
    return num;
}

解决方案

Since you will not post code, here some code for getting roman numerals that always increase. The subtraction part is left to you to figure out. I only post this as it sounds like you are self teaching yourself which is comendable:


int romanToValue(const char c)
{
    switch(c)  // Only works with upper-case as lower case means different things.
    {
      case 'I' : return 1;
      case 'V' : return 5;
      case 'X' : return 10;
      case 'L' : return 50;
      case 'C' : return 100;
      case 'D' : return 500;
      case 'M' : return 1000;
      default: printf("Bad value in string %c\n", c); break;  
    }

    return 0;
}

int romanToInt(const char *str)
{
  int value = 0;
  int i;

  for(i=0; str[i]; i++)  // Dangerous way to do strings, but works for a C example.
  {
    value += romanToValue(str[i]);
  }

  return value;
}

int main(void)
{
  const char cstr[] = "VIII";

  printf("value:%d\n", romanToInt(cstr));
  return 0;
}

Notice the switch statement is working off of char values.

这篇关于无效的转换,从&QUOT;焦炭&QUOT;到&QUOT;字符*&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆