【问题标题】:C: Subscripted value is neither array nor PointerC:下标值既不是数组也不是指针
【发布时间】:2012-03-27 01:44:14
【问题描述】:

我正在做一些 C 类介绍的家庭作业,其中我们必须编写一个程序,该程序从包含酒厂订单信息的文本文件中读取输入。我已经把所有的东西都写出来了,但是当我运行它时,唯一能正确打印的是“Winery #1:”,然后窗口就会出错。我试图在程序结束时打印出我的一个数组以查看问题所在,然后我收到一条错误消息:

|54|error: subscripted value is neither array nor pointer|

我理解错误的含义,但我不确定我需要做些什么来纠正它。我相信我已经正确地声明了我的数组等,但我仍然得到错误。这是我的代码:

int main () {
  //Creates the file pointer and variables
  FILE *ifp;
  int index, index2, index3, index4;
  int wineries, num_bottles, orders, prices, sum_order, total_orders;

  //Opens the file to be read from.
  ifp = fopen ("wine.txt", "r");

  //Scans the first line of the file to find out how many wineries there are,
  //thus finding out how many times the loop must be repeated.
  fscanf(ifp, "%d", &wineries);

  //Begins the main loop which will have repititions equal to the number of wineries.
  for (index = 0; index < wineries; index ++) {
    //Prints the winery number
    printf("Winery #%d:\n", index + 1);

    //Scans the number of bottles at the aforementioned winery and
    //creates the array "prices" which is size "num_bottles."
    fscanf(ifp,"%d", num_bottles );
    int prices[num_bottles];

    //Scans the bottle prices into the array
    for (index2 = 0; index2 < num_bottles; index2++)
      fscanf(ifp, "%d", &prices[index2]);

    //Creates variable orders to scan line 4 into.
    fscanf(ifp, "%d", &orders);

    for(index3 = 0; index3 < orders; index3++){
      int sum_order = 0;

      for(index4 = 0; index4 < num_bottles; index4++)
        fscanf(ifp, "%d", &total_orders);

      sum_order += (prices[index4] * total_orders);
      printf("Order #%d: $%d\n", index3+1, sum_order);
    }
  }
  printf("%d", prices[index2]);
  fclose(ifp);
  return 0;
}

我查看了该网站上的其他一些答案,但似乎没有一个可以帮助我解决我的问题。我有种下沉的感觉,答案就在我的脸上,但作为一个疲惫的业余编码员,我一直无法找到它。提前致谢!

【问题讨论】:

  • prices 没有声明为数组,orders 也没有声明
  • 既然你称自己为业余爱好者,那么一些编码技巧:首先,我希望每个人的代码都这么清晰并且注释得很好!您会发现 cmets 在描述您的推理时最有帮助,而不是代码本身。可以安全地假设任何阅读您的代码的人都理解该语言。他们不能做的是看到你的想法。记录您的假设和期望也是一个好主意,例如,“我们只有在酒用完时才到这里。”
  • 描述性地命名你的变量:而不是index2index3,考虑bottleorder。最后,考虑在forwhileif 语句的单行正文周围使用大括号。这样,当您在正文中添加另一行并欺骗自己认为缩进会说服编译器执行您的意思时,您就不会导致错误。 :-)
  • 错误代码smells像C,像整个程序。你确定问题是 C++ 吗?
  • 感谢您的提示,亚当!是的,我有一位很棒的编程老师,当他告诉我们编写代码时清晰性是多么重要时,他真的很清楚这一点。

标签: c arrays io


【解决方案1】:

有两个prices一个是for循环内的数组,另一个是循环外的int。所以prices[num_bottles] 不再存在,当您尝试在最后打印它时,那里只有 int 价格。显然,int 价格不能用作prices[index2]

从 for 循环中取出价格并将其放在顶部。

【讨论】:

    【解决方案2】:

    改变

    //Scans the number of bottles at the aforementioned winery and
    //creates the array "prices" which is size "num_bottles."
    fscanf(ifp,"%d", num_bottles );
    

    //Scans the number of bottles at the aforementioned winery and
    //creates the array "prices" which is size "num_bottles."
    fscanf(ifp,"%d", &num_bottles );
    //               ^
    

    【讨论】:

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