【问题标题】:Subscripted value is neither array nor pointer nor vector (char positions)下标值既不是数组也不是指针也不是向量(字符位置)
【发布时间】:2015-11-19 16:52:29
【问题描述】:

我已经阅读了一些关于此的帖子,但我仍然无法修复我的代码。

int numericValue(char s, int i) {
    if (strcmp(s[i], "$") == 0)
        return 0;
    else
        return value(s[i]) + numericValue(s, i + 1);
}

这是我的代码,如果我将 int numericValue(char s, int i) 更改为 int numericValue(char *s, int i),我会收到以下警告:

传递 'strcmp' 的参数 1 使指针从整数而不进行强制转换 [-Wint-conversion]。

我能做什么?

【问题讨论】:

  • cad 不是数组,因此您不能使用数组表示法。您想将char * 传递给valorNumerico
  • 请添加您调用valorNumerico的代码行(最初)
  • ...后跟if(cad[i] == '$') 并删除所有不必要的comparacion 东西。
  • 请注意,strcmp() 的第一个参数需要 char *。即使您将函数签名更改为包含char *cadstrcmp() 也不会编译。你需要仔细考虑你在做什么。可能是你需要comparacion = (cad[i] != '$');,但不清楚你在追求什么,所以答案也不清楚。

标签: c


【解决方案1】:

如果您只想比较两个字符,请使用如下所示的双等号 (==),并注意美元符号周围的单引号,因为字符文字包含在单引号中,

int numericValue(char* s, int i) {
if (s[i] == '$')
    return 0;
else
    return value(s[i]) + numericValue(s, i + 1);
}

将使用 strcmp 比较两个字符串,但在您的情况下,您将一个字符作为不匹配的第一个参数传递给 strcmp。请参阅下面的 strcmp 签名:

int strcmp(const char *s1, const char *s2);

【讨论】:

    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多