【问题标题】:Error saying assignment to expression with array type [closed]错误说赋值给数组类型的表达式[关闭]
【发布时间】:2020-04-05 02:28:13
【问题描述】:

我该如何解决这个问题?谢谢

    struct node
    {
    char sym[100];
    }s[20];
    int main()
    {
       for(int i=0;i<5;i++)
       s[i].sym=i;
     return 0;
    }

不确定问题出在哪里,当我使用数组存储整数值/ ascii 值时,出现此错误。为什么呢?对于非数组变量,输入没有错误

【问题讨论】:

  • 您为什么要尝试将int 值存储到数组对象中?你打算如何处理结构中的sym 成员?

标签: c arrays pointers char


【解决方案1】:

您在代码中的有效内容与以下内容没有什么不同:

char sym[100];
sym = 0;

您不能将整数分配给数组。您的代码更有可能使用:

int main() {
    for (int i = 0; i < 5; i++) {
        s[i].sym[SOMETHING] = i;
    }
    return 0;
}

当然,您需要确定SOMETHING 的正确值。

【讨论】:

    【解决方案2】:

    您不能将 int 放入数组 (char)
    试试这个...

    #include <iostream>
    using namespace std;
    
    struct node {
      char sym[100];
    }s;
    
    int main() {
    
      int i = 0;
      for(char a = '1'; a <= '5'; a++){
        s.sym[i] = a; 
        cout << s.sym[i] << " ";
        i++;
      }
    
      cout << endl;
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2020-08-29
      • 2020-12-20
      • 2017-06-12
      • 2015-12-01
      相关资源
      最近更新 更多